SoFunction
Updated on 2025-04-11

Implementation solution of springboot+redis cache

springboot+redis cache scheme

1. Notes

import ;
import ;
import ;
import ;
import ;

/**
 * @author lch
 * @date 2023/8/03 09:29
 */
@Target()
@Retention()
public @interface RedisCache {

    /**
      * key, support spring el expressions
      */
    String key();

    /**
      * Unified format: Service name: Scenario, such as: info
      */
    String prefix();

    /**
      * Expiration time
      */
    int expireTime() default 36000;

    /**
      * Time unit
      */
    TimeUnit timeunit() default ;

}

2. Annotation of facets

import ;
import ;
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;

/**
 * @author lch
 * @date 2023/8/03 09:29
 */
@Component
@Aspect
@Slf4j
public class RedisCacheAspect {

    private final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();

    private final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();

    @Resource
    private RedisService redisService;

    @Around("@annotation(redisCache)")
    public Object queryByRedisCache(ProceedingJoinPoint pjp, RedisCache redisCache) {
        int expireTime = ();
        String key = ();
        String prefix = ();
        (key, "@RedisCache key cannot be empty!");
        (prefix, "@RedisCache prefix cannot be empty!");
        String evaluateExpression = evaluateExpression(key, pjp);

        String redisKey = prefix + evaluateExpression;
        Object obj = null;
        try {
            if (null != redisKey){
                if ((redisKey)){
                    obj = (redisKey);
                    return obj;
                }
            }
        } catch (Exception e) {
            ("Get from Redis"+redisKey+"fail:"+());
        }

        try{
            obj = ();
        }catch(Throwable e){
            ("Execution exception", e);
            throw new RuntimeException(());
        }
        if (null != redisKey){
            (redisKey, obj, expireTime, );
        }

        return obj;
    }

    /**
      * parse el expression
      *
      * @param expression
      * @param point
      * @return
      */
    private String evaluateExpression(String expression, ProceedingJoinPoint point) {
        // Get the target object        Object target = ();
        // Get method parameters        Object[] args = ();
        MethodSignature methodSignature = (MethodSignature) ();
        Method method = ();

        EvaluationContext context = new MethodBasedEvaluationContext(target, method, args, parameterNameDiscoverer);
        Expression exp = (expression);
        return (context, );
    }
}

3. Use examples

    @Override
    @RedisCache(key = "{#customerId + ':' + #dictType}", prefix = CacheConstants.SYS_DICT_KEY)
    public List<SysDictData> selectDictDataByType(Long customerId, String dictType) {
        return (customerId, dictType);
    }

	@Override
    @RedisCache(key = "#apiKey", prefix = CacheConstants.OPEN_KEY + "apiInfo:apiKey:")
    public EdiApiInfo getByApiKey(String apiKey) {
        return (new QueryWrapper<EdiApiInfo>()
                .eq("api_key", apiKey)
                .eq("status", true)
                .eq("del_flag", false));
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.