Create redis cache components
Use hot plug AOP + reflection + redis custom annotations + spring EL expression to create redis cache components, elegantly refactor cache code
Redis configuration
import ; import ; import ; import ; import ; import .GenericJackson2JsonRedisSerializer; import ; @Configuration @EnableAspectJAutoProxy //V2 enable AOP automatic proxypublic class RedisConfig { /** * @param lettuceConnectionFactory * @return * * Redis serialization tool configuration class, please enable configuration below * 127.0.0.1:6379> keys * * 1) "ord:102" serialized * 2) "\xac\xed\x00\x05t\x00\aord:102" Wild, not serialized */ @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); (lettuceConnectionFactory); //Set key serialization method string (new StringRedisSerializer()); //Set the serialization method of value json (new GenericJackson2JsonRedisSerializer()); (new StringRedisSerializer()); (new GenericJackson2JsonRedisSerializer()); (); return redisTemplate; } }
Custom annotations
import ; import ; import ; import ; @Target() @Retention() public @interface MyRedisCache //@EnableAspectJAutoProxy //Enable AOP automatic proxy{ //The prefix is approximately equal to the key, String keyPrefix(); //SpringEL expression, parse the matching value corresponding to the placeholder. String matchValue(); }
AOP
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Component @Aspect public class MyRedisCacheAspect { @Resource private RedisTemplate redisTemplate; //Configure the entry point @Pointcut("@annotation(.)") public void cachePointCut(){} @Around("cachePointCut()") public Object doCache(ProceedingJoinPoint joinPoint) { Object result = null; /** * @MyRedisCache(keyPrefix = "user",matchValue = "#id") * public User getUserById(Integer id) * { * return (id); * } */ try { //1 Get the overloaded method name MethodSignature signature = (MethodSignature) (); Method method = (); //2 After determining the method name, obtain the annotation tag configured above the method MyRedisCache MyRedisCache myRedisCacheAnnotation = (); //3 Get the MyRedisCache annotation tag and get the parameters configured on the annotation for encapsulation and calling String keyPrefix = (); String matchValueSpringEL = (); //4 SpringEL parser ExpressionParser parser = new SpelExpressionParser(); Expression expression = (matchValueSpringEL);//#id EvaluationContext context = new StandardEvaluationContext(); //5 Get the number of formal parameters in the method Object[] args = (); DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); String[] parameterNames = (method); for (int i = 0; i < ; i++) { ("Get the parameter name and value in the method: "+parameterNames[i] + "\t" + args[i].toString()); (parameterNames[i], args[i].toString()); } //6 Through the above, splicing the final key form of redis String key = keyPrefix + ":" + (context).toString(); ("--------The final key form of splicing redis: " + key); //7 First check if there is any in redis result = ().get(key); if (result != null) { ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + result); return result; } //8 There is no redis, go to msyql query or follow-up business logic //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //(id); result = ();//The main business logic query mysql, release and release //9 MySQL step ends, and the result needs to be stored in Redis once, and the cache compensation needs to be compensated. if (result != null) { ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + result); ().set(key, result); } } catch (Throwable throwable) { (); } return result; } }
test
/** * The return value will be stored in redis. The key generation rules require the programmer to specify themselves with SpEL expressions. The value is the user that the program finds out from mysql and returns. * redis's key equals keyPrefix:matchValue */ @Override @MyRedisCache(keyPrefix = "user",matchValue = "#id") public User getUserById(Integer id) { return (id); }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.