瀏覽代碼

improve:原生redis缓存写法

lileilei 2 年之前
父節點
當前提交
1b876f9932

+ 0 - 50
src/main/java/com/rshy/project/hy/aop/NoRepeatSubmitAop.java

@@ -1,50 +0,0 @@
-package com.rshy.project.hy.aop;
-
-import com.rshy.project.hy.annotation.NoRepeatSubmit;
-import com.rshy.project.hy.baseRe.Ret;
-import com.rshy.project.hy.cache.CacheUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
-import org.aspectj.lang.annotation.Aspect;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
-
-import javax.servlet.http.HttpServletRequest;
-
-@Aspect
-@Component
-@Slf4j
-public class NoRepeatSubmitAop {
-    @Autowired
-    private CacheUtil cacheUtil;
-
-    private static final String CACHE_NAME = "";
-
-    @Around("execution(* com.rshy..*Controller.*(..)) && @annotation(nrs)")
-    public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) throws Throwable{
-        try {
-            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-            String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
-            HttpServletRequest request = attributes.getRequest();
-            String key = sessionId + "-" + request.getServletPath();
-            // 如果缓存中有这个url视为重复提交
-            if (cacheUtil.get(CACHE_NAME, key) == null) {
-                Object o = pjp.proceed();
-                cacheUtil.put(CACHE_NAME, key,0,2);
-                return o;
-            } else {
-                return Ret.error(10003,"请勿频繁提交");
-            }
-        } catch (Throwable e) {
-            e.printStackTrace();
-            log.error("验证重复提交时出现未知异常!",e);
-            throw e;
-        }
-
-    }
-
-
-}

+ 0 - 47
src/main/java/com/rshy/project/hy/cache/CacheUtil.java

@@ -1,47 +0,0 @@
-package com.rshy.project.hy.cache;
-
-import cn.hutool.extra.spring.SpringUtil;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-/**
- * @author dxy
- * @description: 缓存工具类
- * @date 2021/4/5 16:48
- */
-@Component
-public class CacheUtil {
-    @Autowired
-    private StarshowCacheManager starshowCacheManager;
-
-    private ICache getCache() {
-        return starshowCacheManager.getCache();
-    }
-
-    public <T> T get(String cacheName, Object key) {
-        return getCache().get(cacheName, key);
-    }
-
-    public void put(String cacheName, Object key, Object value) {
-        getCache().put(cacheName, key, value);
-    }
-
-    public void put(String cacheName, Object key, Object value, int liveSeconds) {
-        getCache().put(cacheName, key, value, liveSeconds);
-    }
-
-    public void remove(String cacheName, Object key) {
-        getCache().remove(cacheName, key);
-    }
-
-    public void removeAll(String cacheName) {
-        getCache().removeAll(cacheName);
-    }
-
-    public String genCacheKey(String prefix, Object keyValue) {
-        return prefix + keyValue;
-    }
-
-
-
-}

+ 0 - 49
src/main/java/com/rshy/project/hy/cache/ICache.java

@@ -1,49 +0,0 @@
-package com.rshy.project.hy.cache;
-
-/**
- * @author dxy
- * @description: 缓存接口
- * @date 2021/4/5 16:48
- */
-public interface ICache {
-
-    /**
-     * 读缓存
-     * @param cacheName
-     * @param key
-     * @param <T>
-     * @return
-     */
-    <T> T get(String cacheName, Object key);
-
-    /**
-     * 写缓存
-     * @param cacheName
-     * @param key
-     * @param value
-     */
-    void put(String cacheName, Object key, Object value);
-
-    /**
-     * 写缓存,缓存时间,秒
-     * @param cacheName
-     * @param key
-     * @param value
-     * @param liveSeconds
-     */
-    void put(String cacheName, Object key, Object value, int liveSeconds);
-
-    /**
-     * 删除缓存
-     * @param cacheName
-     * @param key
-     */
-    void remove(String cacheName, Object key);
-
-    /**
-     * 删除所有cacheName下缓存
-     * @param cacheName
-     */
-    void removeAll(String cacheName);
-
-}

+ 0 - 22
src/main/java/com/rshy/project/hy/cache/StarshowCacheManager.java

@@ -1,22 +0,0 @@
-package com.rshy.project.hy.cache;
-
-import com.rshy.project.hy.cache.redis.RedisCacheImpl;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-/**
- * @author dxy
- * @description: 缓存统一管理器
- * @date 2021/4/5 16:48
- */
-@Component
-public class StarshowCacheManager {
-
-    @Autowired
-    RedisCacheImpl redisCache;
-
-    public ICache getCache() {
-        return redisCache;
-    }
-
-}

+ 0 - 309
src/main/java/com/rshy/project/hy/cache/redis/RedisCacheImpl.java

@@ -1,309 +0,0 @@
-package com.rshy.project.hy.cache.redis;
-
-import com.rshy.project.hy.cache.ICache;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-/**
- * @author dxy
- * @description: redis缓存方法模板
- * @date 2021/4/5 16:48
- */
-@Slf4j
-@Component
-public class RedisCacheImpl implements ICache {
-
-    @Autowired
-    private RedisTemplate<Object,Object> redisTemplate;
-
-    @SuppressWarnings("unchecked")
-    @Override
-    public <T> T get(String cacheName, Object key) {
-        return (T) redisTemplate.opsForValue().get(buildKey(cacheName, key));
-    }
-
-    @Override
-    public void put(String cacheName, Object key, Object value) {
-        redisTemplate.opsForValue().set(buildKey(cacheName, key), value);
-    }
-
-    @Override
-    public void put(String cacheName, Object key, Object value, int liveSeconds) {
-        redisTemplate.opsForValue().set(buildKey(cacheName, key), value, liveSeconds, TimeUnit.SECONDS);
-    }
-
-    @Override
-    public void remove(String cacheName, Object key) {
-        redisTemplate.delete(buildKey(cacheName, key));
-    }
-
-    @Override
-    public void removeAll(String cacheName) {
-        Set<Object> keys = redisTemplate.keys(cacheName + ":*");
-        if (keys == null) {
-            return;
-        }
-        if (keys.size() > 0) {
-            redisTemplate.delete(keys);
-        }
-    }
-
-    public synchronized Boolean putLock(String cacheName, long liveSeconds) {
-        return redisTemplate.opsForValue().setIfAbsent(cacheName, 1, liveSeconds, TimeUnit.SECONDS);
-    }
-
-    public synchronized Boolean unLock(String cacheName) {
-        return redisTemplate.delete(cacheName);
-    }
-
-    private String buildKey(String cacheName, Object key) {
-        return String.format("%s::%s", cacheName, key);
-    }
-
-    /**
-     * 设置有效时间
-     *
-     * @param key Redis键
-     * @param timeout 超时时间
-     * @return true=设置成功;false=设置失败
-     */
-    public  boolean expire(final String key, final long timeout) {
-
-        return expire(key, timeout, TimeUnit.SECONDS);
-    }
-
-    /**
-     * 设置有效时间
-     *
-     * @param key Redis键
-     * @param timeout 超时时间
-     * @param unit 时间单位
-     * @return true=设置成功;false=设置失败
-     */
-    public  boolean expire(final String key, final long timeout, final TimeUnit unit) {
-
-        Boolean ret = redisTemplate.expire(key, timeout, unit);
-        return ret != null && ret;
-    }
-
-    /**
-     * 删除单个key
-     *
-     * @param key 键
-     * @return true=删除成功;false=删除失败
-     */
-    public  boolean del(final String key) {
-        Boolean ret = redisTemplate.delete(key);
-        return ret != null && ret;
-    }
-
-    /**
-     * 删除多个key
-     *
-     * @param keys 键集合
-     * @return 成功删除的个数
-     */
-    public boolean del(final Collection<String> keys) {
-        Boolean ret = redisTemplate.delete(keys);
-        return ret == null ? null : ret;
-    }
-
-    /**
-     * 存入普通对象
-     *
-     * @param key Redis键
-     * @param value 值
-     */
-    public  void set(final String key, final Object value) {
-        redisTemplate.opsForValue().set(key, value);
-    }
-
-    /**
-     * 递增
-     *
-     * @param key Redis键
-     * @param value 值
-     */
-    public  void incr(final String key, final long value) {
-        redisTemplate.opsForValue().increment(key, value);
-    }
-
-
-    /**
-     * 递增
-     *
-     * @param key Redis键
-     * @param value 值
-     */
-    public  void decr(final String key, final long value) {
-        redisTemplate.opsForValue().decrement(key, value);
-    }
-
-    // 存储普通对象操作
-
-    /**
-     * 存入普通对象
-     *
-     * @param key 键
-     * @param value 值
-     * @param timeout 有效期,单位秒
-     */
-    public  void set(final String key, final Object value, final long timeout) {
-        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
-    }
-
-    /**
-     * 存入字符串对象
-     *
-     * @param key 键
-     * @param value 值
-     * @param timeout 有效期,单位秒
-     */
-    public  void setString(final String key, final Object value, final long timeout) {
-        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
-    }
-
-    /**
-     * 获取普通对象
-     *
-     * @param key 键
-     * @return 对象
-     */
-    public  Object get(final String key) {
-        return redisTemplate.opsForValue().get(key);
-    }
-
-    // 存储Hash操作
-
-    /**
-     * 往Hash中存入数据
-     *
-     * @param key Redis键
-     * @param hKey Hash键
-     * @param value 值
-     */
-    public  void hPut(final String key, final String hKey, final Object value) {
-        redisTemplate.opsForHash().put(key, hKey, value);
-    }
-
-    public  void hIncr(final String key, final String hKey, final long value) {
-        redisTemplate.opsForHash().increment(key, hKey, value);
-    }
-
-
-    /**
-     * 往Hash中存入多个数据
-     *
-     * @param key Redis键
-     * @param values Hash键值对
-     */
-    public  void hPutAll(final String key, final Map<String, Object> values) {
-        redisTemplate.opsForHash().putAll(key, values);
-    }
-
-    /**
-     * 获取Hash中的数据
-     *
-     * @param key Redis键
-     * @param hKey Hash键
-     * @return Hash中的对象
-     */
-    public  Object hGet(final String key, final String hKey) {
-        return redisTemplate.opsForHash().get(key, hKey);
-    }
-
-    /**
-     * 获取多个Hash中的数据
-     *
-     * @param key Redis键
-     * @param hKeys Hash键集合
-     * @return Hash对象集合
-     */
-    public List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {
-        return redisTemplate.opsForHash().multiGet(key, hKeys);
-    }
-
-    // 存储Set相关操作
-
-    /**
-     * 往Set中存入数据
-     *
-     * @param key Redis键
-     * @param values 值
-     * @return 存入的个数
-     */
-    public  long sSet(final String key, final Object... values) {
-        Long count = redisTemplate.opsForSet().add(key, values);
-        return count == null ? 0 : count;
-    }
-
-    /**
-     * 删除Set中的数据
-     *
-     * @param key Redis键
-     * @param values 值
-     * @return 移除的个数
-     */
-    public  long sDel(final String key, final Object... values) {
-        Long count = redisTemplate.opsForSet().remove(key, values);
-        return count == null ? 0 : count;
-    }
-
-    // 存储List相关操作
-
-    /**
-     * 往List中存入数据
-     *
-     * @param key Redis键
-     * @param value 数据
-     * @return 存入的个数
-     */
-    public  long lPush(final String key, final Object value) {
-        Long count = redisTemplate.opsForList().rightPush(key, value);
-        return count == null ? 0 : count;
-    }
-
-    /**
-     * 往List中存入多个数据
-     *
-     * @param key Redis键
-     * @param values 多个数据
-     * @return 存入的个数
-     */
-    public  long lPushAll(final String key, final Collection<Object> values) {
-        Long count = redisTemplate.opsForList().rightPushAll(key, values);
-        return count == null ? 0 : count;
-    }
-
-    /**
-     * 往List中存入多个数据
-     *
-     * @param key Redis键
-     * @param values 多个数据
-     * @return 存入的个数
-     */
-    public  long lPushAll(final String key, final Object... values) {
-        Long count = redisTemplate.opsForList().rightPushAll(key, values);
-        return count == null ? 0 : count;
-    }
-
-    /**
-     * 从List中获取begin到end之间的元素
-     *
-     * @param key Redis键
-     * @param start 开始位置
-     * @param end 结束位置(start=0,end=-1表示获取全部元素)
-     * @return List对象
-     */
-    public  List<Object> lGet(final String key, final int start, final int end) {
-        return redisTemplate.opsForList().range(key, start, end);
-    }
-}

+ 7 - 7
src/main/java/com/rshy/project/hy/server/ItfwServer.java

@@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
-import com.rshy.project.hy.cache.CacheUtil;
 import com.rshy.project.hy.config.properties.*;
 import com.rshy.project.hy.model.constant.ItfwConstant;
 import com.rshy.project.hy.server.dto.*;
@@ -15,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Component;
 
 
@@ -45,7 +45,7 @@ public class ItfwServer {
     @Autowired
     private PctqkryProperties pctqkryProperties;
     @Autowired
-    private CacheUtil cacheUtil;
+    private StringRedisTemplate stringRedisTemplate;
 
     /**
      * 基础服务访问
@@ -71,10 +71,10 @@ public class ItfwServer {
             return null;
         }
 
-        Object cache = cacheUtil.get(ItfwConstant.BARCODE_KEY, barCode);
-        if(null != cache){
-            log.info("健康码服务走缓存读取,响应内容: {}", cache.toString());
-            return JSON.parseObject(cache.toString(), PctjkmInfoDTO.class);
+        String cache = stringRedisTemplate.opsForValue().get(ItfwConstant.BARCODE_KEY.concat(barCode));
+        if(StrUtil.isNotEmpty(cache)){
+            log.info("健康码服务走缓存读取,响应内容: {}", cache);
+            return JSON.parseObject(cache, PctjkmInfoDTO.class);
         }
 
         PctjkmProperties properties = new PctjkmProperties();
@@ -94,7 +94,7 @@ public class ItfwServer {
         }
 
         PctjkmInfoDTO pctjkmInfoDTO = pctjkmVO.getData().getResult().stream().findFirst().get();
-        cacheUtil.put(ItfwConstant.BARCODE_KEY, barCode, JSON.toJSONString(pctjkmInfoDTO));
+        stringRedisTemplate.opsForValue().set(ItfwConstant.BARCODE_KEY.concat(barCode), JSON.toJSONString(pctjkmInfoDTO));
         return pctjkmInfoDTO;
     }
 

+ 4 - 4
src/main/java/com/rshy/project/hy/web/controller/CacheController.java

@@ -1,11 +1,11 @@
 package com.rshy.project.hy.web.controller;
 
 import com.alibaba.fastjson.JSON;
-import com.rshy.project.hy.cache.CacheUtil;
 import com.rshy.project.hy.model.constant.ItfwConstant;
 import com.rshy.project.hy.server.dto.PctjkmInfoDTO;
 import io.swagger.annotations.Api;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -20,11 +20,11 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/cache")
 public class CacheController {
     @Autowired
-    private CacheUtil cacheUtil;
+    private StringRedisTemplate stringRedisTemplate;
 
     @GetMapping
     public String test(String barCode){
-        cacheUtil.put(ItfwConstant.BARCODE_KEY, barCode, JSON.toJSONString(new PctjkmInfoDTO().setPn("18668177616").setId_num("342224199411110777")));
-        return cacheUtil.get(ItfwConstant.BARCODE_KEY, barCode).toString();
+        stringRedisTemplate.opsForValue().set(ItfwConstant.BARCODE_KEY.concat(barCode), JSON.toJSONString(new PctjkmInfoDTO().setPn("18668177616").setId_num("342224199411110777")));
+        return stringRedisTemplate.opsForValue().get(ItfwConstant.BARCODE_KEY.concat(barCode));
     }
 }