自定义redis缓存注解

1、自定义缓存注解使用示例

@Override
@CustomRedisCache(key = "app:course:hot:sale")
public List<CourseRspVO> getHotSaleCourseList(@CustomReidsParam Long areaId) {

   List<CourseRspVO> courseRspVOList = this.getCourseRspVOListByParam(areaId);

   return courseRspVOList;
}

@Override
@CustomRedisCache(key = "app:course:hot:top")
public List<CourseRspVO> getTopCourseList(@CustomReidsParam Long areaId) {

   List<CourseRspVO> courseRspVOList = this.getCourseRspVOListByParam(areaId);

   return courseRspVOList;
}

2、自定注解,作用于方法上

/**
 * @Deacription TODO
 * @Author jianhua.hong
 * @Date 2019/12/5 15:30
 **/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomRedisCache {

   /**
    * 缓存key
    **/
   String key();

   /**
    * 缓存时间
    **/
   int expireTime() default 3600;

   /**
    * 缓存时间单位,默认为秒
    **/
   TimeUnit timeUnit() default TimeUnit.SECONDS;

}

3、自定义注解,作用于参数上

/**
 * @Deacription TODO
 * @Author jianhua.hong
 * @Date 2019/12/12 17:09
 **/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomReidsParam {

}

4、缓存注解拦截器

@Slf4j
@Aspect
@Component
public class CustomRedisCacheAspect {

   private static final String KEY_SPILT = ":";

   @Resource
   private RedisService redisService;

   @Around("@annotation(com.trjcn.common.redis.CustomRedisCache)")
   public Object handleCache(ProceedingJoinPoint point) throws Throwable {
      try {
         Method method = this.getMethod(point);

         Object[] objects = point.getArgs();

         CustomRedisCache customRedisCache = method.getAnnotation(CustomRedisCache.class);

         String key = customRedisCache.key();

         String fulKey = this.buildFullKey(method, key, objects);

         int expireTime = customRedisCache.expireTime();

         TimeUnit timeUnit = customRedisCache.timeUnit();

         if (redisService.hasKey(key)) {

            log.info("通过自定义缓存,获取key=[{}]数据", fulKey);

            Object o = redisService.getObject(fulKey);

            return o;

         } else {

            Object result = point.proceed();

            if (!ObjectUtils.isEmpty(result)) {

               redisService.put(fulKey, result, expireTime, timeUnit);

               log.info("通过自定义缓存,缓存数据key=[{}]", fulKey);
            }


         }
      } catch (Exception e) {
         log.error("handleCache error,JoinPoint:{}", point.getSignature(), e);
      }
      return point.proceed();
   }


   private String buildFullKey(Method method, String key, Object[] objects) {

      Parameter[] parameters = method.getParameters();

      if (parameters == null || parameters.length <= 0) {
         return key;
      }


      for (int i = 0; i < parameters.length; i++) {

         boolean isCustomRedisField = parameters[i].isAnnotationPresent(CustomReidsParam.class);

         if (isCustomRedisField) {

            Object value = objects[i];

            if (value != null) {

               String paramKey = value.toString();
               
               key = key + KEY_SPILT + paramKey;

            }
         }
      }
      return key;
   }

   public Object returnConvert(ProceedingJoinPoint point, String str) {

      MethodSignature methodSignature = (MethodSignature) point.getSignature();

      Class returnClass = methodSignature.getReturnType();

      Object o = JSONObject.parseArray(str, returnClass);

      return o;
   }

   private Method getMethod(JoinPoint joinPoint) throws Exception {

      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

      Method method = methodSignature.getMethod();

      return method;
   }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容