use of com.hccake.ballcat.common.redis.operation.CachedOps in project ballcat by ballcat-projects.
the class CacheStringAspect method around.
@Around("pointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
// 获取目标方法
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
log.trace("=======The string cache aop is executed! method : {}", method.getName());
// 根据方法的参数 以及当前类对象获得 keyGenerator
Object target = point.getTarget();
Object[] arguments = point.getArgs();
KeyGenerator keyGenerator = new KeyGenerator(target, method, arguments);
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
// 获取注解对象
Cached cachedAnnotation = AnnotationUtils.getAnnotation(method, Cached.class);
if (cachedAnnotation != null) {
// 缓存key
String key = keyGenerator.getKey(cachedAnnotation.key(), cachedAnnotation.keyJoint());
// redis 分布式锁的 key
String lockKey = key + CachePropertiesHolder.lockKeySuffix();
Supplier<String> cacheQuery = () -> valueOperations.get(key);
// 失效时间控制
Consumer<Object> cachePut = prodCachePutFunction(valueOperations, key, cachedAnnotation.ttl());
return cached(new CachedOps(point, lockKey, cacheQuery, cachePut, method.getGenericReturnType()));
}
CachePut cachePutAnnotation = AnnotationUtils.getAnnotation(method, CachePut.class);
if (cachePutAnnotation != null) {
// 缓存key
String key = keyGenerator.getKey(cachePutAnnotation.key(), cachePutAnnotation.keyJoint());
// 失效时间控制
Consumer<Object> cachePut = prodCachePutFunction(valueOperations, key, cachePutAnnotation.ttl());
return cachePut(new CachePutOps(point, cachePut));
}
CacheDel cacheDelAnnotation = AnnotationUtils.getAnnotation(method, CacheDel.class);
if (cacheDelAnnotation != null) {
VoidMethod cacheDel;
if (cacheDelAnnotation.multiDel()) {
Collection<String> keys = keyGenerator.getKeys(cacheDelAnnotation.key(), cacheDelAnnotation.keyJoint());
cacheDel = () -> redisTemplate.delete(keys);
} else {
// 缓存key
String key = keyGenerator.getKey(cacheDelAnnotation.key(), cacheDelAnnotation.keyJoint());
cacheDel = () -> redisTemplate.delete(key);
}
return cacheDel(new CacheDelOps(point, cacheDel));
}
return point.proceed();
}
Aggregations