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;
}
}