Search in sources :

Example 1 with CachePut

use of com.hccake.ballcat.common.redis.core.annotation.CachePut 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();
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) CachedOps(com.hccake.ballcat.common.redis.operation.CachedOps) CacheDelOps(com.hccake.ballcat.common.redis.operation.CacheDelOps) CacheDel(com.hccake.ballcat.common.redis.core.annotation.CacheDel) VoidMethod(com.hccake.ballcat.common.redis.operation.function.VoidMethod) Method(java.lang.reflect.Method) CachePut(com.hccake.ballcat.common.redis.core.annotation.CachePut) Cached(com.hccake.ballcat.common.redis.core.annotation.Cached) VoidMethod(com.hccake.ballcat.common.redis.operation.function.VoidMethod) CachePutOps(com.hccake.ballcat.common.redis.operation.CachePutOps) Around(org.aspectj.lang.annotation.Around)

Aggregations

CacheDel (com.hccake.ballcat.common.redis.core.annotation.CacheDel)1 CachePut (com.hccake.ballcat.common.redis.core.annotation.CachePut)1 Cached (com.hccake.ballcat.common.redis.core.annotation.Cached)1 CacheDelOps (com.hccake.ballcat.common.redis.operation.CacheDelOps)1 CachePutOps (com.hccake.ballcat.common.redis.operation.CachePutOps)1 CachedOps (com.hccake.ballcat.common.redis.operation.CachedOps)1 VoidMethod (com.hccake.ballcat.common.redis.operation.function.VoidMethod)1 Method (java.lang.reflect.Method)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1