Search in sources :

Example 1 with CacheDel

use of com.hccake.ballcat.common.redis.core.annotation.CacheDel 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)

Example 2 with CacheDel

use of com.hccake.ballcat.common.redis.core.annotation.CacheDel in project ballcat by ballcat-projects.

the class I18nDataServiceImpl method saveOrUpdate.

@Override
@CacheDel(key = I18nRedisKeyConstants.I18N_DATA_PREFIX, multiDel = true, keyJoint = "#p0.![#this.code + ':' + #this.languageTag]")
public void saveOrUpdate(List<I18nData> list) {
    // 查询已存在的数据
    List<I18nData> existsI18nData = baseMapper.exists(list);
    HashSet<I18nData> existsSet = new HashSet<>(existsI18nData);
    // 获取对应插入和更新的列表
    List<I18nDataDTO> updateList = new ArrayList<>();
    List<I18nData> insertList = new ArrayList<>();
    for (I18nData i18nData : list) {
        if (existsSet.contains(i18nData)) {
            updateList.add(I18nDataConverter.INSTANCE.poToDto(i18nData));
        } else {
            insertList.add(i18nData);
        }
    }
    // 小范围事务处理,另外不影响缓存更新
    i18NDataTxSupport.saveAndUpdate(insertList, updateList);
    // 缓存更新
    for (I18nDataDTO i18nDataDTO : updateList) {
        String code = i18nDataDTO.getCode();
        String languageTag = i18nDataDTO.getLanguageTag();
        this.pushUpdateMessage(code, languageTag);
    }
}
Also used : ArrayList(java.util.ArrayList) I18nDataDTO(com.hccake.ballcat.i18n.model.dto.I18nDataDTO) I18nData(com.hccake.ballcat.i18n.model.entity.I18nData) HashSet(java.util.HashSet) CacheDel(com.hccake.ballcat.common.redis.core.annotation.CacheDel)

Aggregations

CacheDel (com.hccake.ballcat.common.redis.core.annotation.CacheDel)2 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 I18nDataDTO (com.hccake.ballcat.i18n.model.dto.I18nDataDTO)1 I18nData (com.hccake.ballcat.i18n.model.entity.I18nData)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1