In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Springboot Cache @ CacheEvict can not fuzzy delete how to do", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Springboot Cache @ CacheEvict can not fuzzy delete how to do" it!
SpringbootCache @ CacheEvict cannot be blurred deleted
Deleting the cache with @ CacheEvict can only delete the cache of the specified key. In some cases, when you need to delete all key based on the prefix, you cannot use @ CacheEvict, so we customize an @ CacheRemove to handle fuzzy deletion of all cache based on the prefix (Spring EL expressions are supported).
The following code applies to Redis
Add dependency
Org.springframework.boot spring-boot-starter-aop
Startup class plus @ EnableAspectJAutoProxy
@ CacheRemove code
Package com.marssvn.utils.annotation.cache; import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; @ Target ({METHOD}) @ Retention (RetentionPolicy.RUNTIME) public @ interface CacheRemove {String [] value () default {};}
CacheRemoveAspect AOP implementation class code
Package com.marssvn.utils.annotation.cache.aspect; import com.marssvn.utils.annotation.cache.CacheRemove;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.LocalVariableTableParameterNameDiscoverer;import org.springframework.data.redis.core.StringRedisTemplate;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 javax.annotation.Resource;import java.lang.reflect.Method;import java.util.Set; @ Aspect@Componentpublic class CacheRemoveAspect {@ Resource private StringRedisTemplate stringRedisTemplate; private Logger logger = LoggerFactory.getLogger (this.getClass ()) AfterReturning ("@ annotation (com.marssvn.utils.annotation.cache.CacheRemove)") public void remove (JoinPoint point) {Method method = ((MethodSignature) point.getSignature ()) .getMethod (); CacheRemove cacheRemove = method.getAnnotation (CacheRemove.class); String [] keys = cacheRemove.value () For (String key: keys) {if (key.contains ("#")) key = parseKey (key, method, point.getArgs ()); Set deleteKeys = stringRedisTemplate.keys (key); stringRedisTemplate.delete (deleteKeys); logger.info ("cache key:" + key + "deleted") }} / * parseKey from SPEL * / private String parseKey (String key, Method method, Object [] args) {LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer (); String [] paraNameArr = u.getParameterNames (method); ExpressionParser parser = new SpelExpressionParser (); StandardEvaluationContext context = new StandardEvaluationContext (); for (int I = 0; I
< paraNameArr.length; i++) { context.setVariable(paraNameArr[i], args[i]); } return parser.parse_Expression(key).getValue(context, String.class); }} Service中的调用代码 /** * Delete repository * * @param id repositoryId */ @Override @Transactional @CacheRemove({"repository.list::*", "'repository::id=' + #id", "'repository.tree::id=' + #id + '*'"}) public void deleteRepositoryById(int id) { // business code }@CacheEvict根据缓存名称模糊删除@CacheEvict(cacheNames = "likename" ,allEntries=true) allEntries=true 开启全匹配 cacheNames 填写 模糊删除的name 看源码可知Thank you for your reading, the above is the content of "Springboot Cache @ CacheEvict can not vaguely delete how to do", after the study of this article, I believe you can not fuzzy delete Springboot Cache @ CacheEvict how to do this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.