1. 程式人生 > 其它 >對redis快取使用的註解--針對list型別

對redis快取使用的註解--針對list型別

我們使用redis快取大多數用的是差不多的模板,程式碼侵入性大,此處加個註解,方便使用。

註解:

package com.sd.outbound.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 註解 ListCacheData 用於簡便處理需要進行快取的操作
 * 注意 增加了全域性快取開關,引數為 global_list_cache_open_status_key,
 * 若是要關閉所有使用這個註解的快取,可在nacos 配置中心或者其他配置檔案配置 global_list_cache_open_status_key: false 即可關閉
 * 若是要關閉單個使用註解快取的地方,在nacos 配置中心或者其他配置檔案的地方 配置 prefix: false 即可
 
*/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ListCacheData { /** * 字首 * @return */ String prefix(); /** * 作為key的引數是在方法的第幾個引數 * @return */ int suffixParamIndex() default 0; /** * 字尾欄位,,為空時候表示不需要字尾表示式,採用 SPEL * eg: * 1.不支援加固定字尾!!! 目前spEl資料來源是來自方法入參,要是需要固定字尾,放置在字首就好了; * 2.訪問引數 比如 methodName(List<String userId, String pageId) ->"#userId+'_'+#pageId"; * 3.訪問物件內屬性 比如 methodName(UserBO userBO) ->"#userBO.name"; * 4.訪問集合資料 比如 methodName(List<String> list) -> "#list.toString()"; * *
@return */ String suffixExpression() default ""; /** * 字尾欄位,,為空時候表示不需要字尾表示式,採用 SPEL * eg: * 1.不支援加固定字尾!!! 目前spEl資料來源是來自方法入參,要是需要固定字尾,放置在字首就好了; * 2.訪問引數 比如 methodName(List<String userId, String pageId) ->"#userId+'_'+#pageId"; * 3.訪問物件內屬性 比如 methodName(UserBO userBO) ->"#userBO.name"; * 4.訪問集合資料 比如 methodName(List<String> list) -> "#list.toString()"; * *
@return */ String suffixField(); /** * 快取時間 秒 * @return */ long expireSecond() default 300; }

切面:

package com.sd.outbound.core.aspect;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.sd.outbound.common.annotation.ListCacheData;
import com.sd.outbound.core.CoreConstants;
import com.sd.outbound.core.domain.bo.UserBO;
import com.soudian.common.StringHelper;
import com.soudian.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Slf4j
@Aspect
@Component
public class ListCacheDataAspect {

    private ExpressionParser parser = new SpelExpressionParser();

    private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Environment env;

    @Around("@annotation(listCacheData)")
    public Object listCacheDataAround(ProceedingJoinPoint pjp, ListCacheData listCacheData) throws Throwable {
        String prefix = listCacheData.prefix();

        String currentCacheOpenStatus = env.getProperty(prefix);
        String globalCacheStatus = env.getProperty(CoreConstants.GLOBAL_LIST_CACHE_OPEN_STATUS_KEY);
        log.info("listCacheOpenStatus of {} = {}, globalListCacheStatus={}", prefix, currentCacheOpenStatus, globalCacheStatus);
        if (Boolean.FALSE.toString().toLowerCase().equals(globalCacheStatus) || Boolean.FALSE.toString().toLowerCase().equals(currentCacheOpenStatus)) {
            return pjp.proceed();
        }

        long expireSecond = listCacheData.expireSecond();
        int suffixParamIndex = listCacheData.suffixParamIndex();

        Method method = getMethod(pjp);
        Type genericReturnType = method.getGenericReturnType();
        Class<?> returnType = method.getReturnType();

        Object arg = null;
        if (suffixParamIndex >= pjp.getArgs().length || (arg = pjp.getArgs()[suffixParamIndex]) == null) {
            return pjp.proceed();
        }

        if (arg instanceof List) {
            List list = (List) arg;
            if (CollectionUtils.isEmpty(list)) {
                return pjp.proceed();
            }

            List cachedList = new ArrayList();
            Map mapResult = new HashMap();
            List listResult = new ArrayList();
            Boolean isMap = false;
            if(returnType.getSimpleName().equals("Map")){
                isMap = true;
            }

                for (Object o : list) {
                    String key = StrUtil.join(StrUtil.COLON, prefix, parseSpel(o, listCacheData.suffixExpression()));

                    String data = stringRedisTemplate.opsForValue().get(key);
                    if (!StringHelper.isEmpty(data)) {
                        cachedList.add(o);
                        Object itemResult = JSON.parseObject(data, genericReturnType);
                        if(isMap){
                            Map itemMap = (Map) itemResult;
                            mapResult.put(o, itemMap.get(o));
                        }else{
                            listResult.add(itemResult);
                        }
                    }
                }

            if (cachedList.size() < ((List<?>) arg).size()) {
                for (Object o : cachedList) {
                    ((List<?>) arg).remove(o);
                }

                Object result = pjp.proceed();
                if(result instanceof Map){
                    Map dbMapResult = (Map) result;
                    if(CollectionUtils.isEmpty(dbMapResult)){
                        return mapResult;
                    }

                    for (Object o : dbMapResult.keySet()) {
                        Map map = new HashMap(1, 1);
                        map.put(o, dbMapResult.get(o));
                        mapResult.put(o, dbMapResult.get(o));

                        stringRedisTemplate.opsForValue().set(StrUtil.join(StrUtil.COLON, prefix, parseSpel(o, listCacheData.suffixExpression())), JSON.toJSONString(map), expireSecond, TimeUnit.SECONDS);
                    }
                }else if(result instanceof List){
                    List dbListResult = (List) result;
                    if(CollectionUtils.isEmpty(dbListResult)){
                        return listResult;
                    }

                    for (Object o : dbListResult) {
                        stringRedisTemplate.opsForValue().set(StrUtil.join(StrUtil.COLON, prefix, parseSpel(o, listCacheData.suffixField())), JSON.toJSONString(Arrays.asList(o)), expireSecond, TimeUnit.SECONDS);
                        listResult.add(o);
                    }
                }

                log.info("CacheData get data from db in method:{}", method.getName());
                    log.info("listCacheDataAround pjp absentList = {}", arg);
            }else {
                return isMap ? mapResult: listResult;
            }
        }

        return pjp.proceed();
    }

    private Object parseSpel(Object item, String spel) {
        if(StringHelper.isEmpty(spel)){
            return item;
        }
        spel = "#item."+spel;
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("item", item);
        try {
            Expression expression = parser.parseExpression(spel);
            return expression.getValue(context);
        } catch (Exception e) {
            return StrUtil.EMPTY;
        }
    }

    private static Method getMethod(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        if (method.getDeclaringClass().isInterface()) {
            try {
                method = pjp
                        .getTarget()
                        .getClass()
                        .getDeclaredMethod(pjp.getSignature().getName(),
                                method.getParameterTypes());
            } catch (SecurityException | NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }

        return method;
    }
}