您好,登錄后才能下訂單哦!
這篇文章主要講解了“Springboot Cache @CacheEvict無法模糊刪除怎么辦”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Springboot Cache @CacheEvict無法模糊刪除怎么辦”吧!
用@CacheEvict刪除緩存只能刪除指定key的緩存,有些情況需要根據前綴刪除所有key的時候,用@CacheEvict就做不到了,所以我們自定義一個@CacheRemove來處理根據前綴模糊刪除所有cache(支持Spring EL表達式)
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
啟動類加上 @EnableAspectJAutoProxy
@CacheRemove 代碼
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實現類代碼
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 @Component public 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<String> 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.parseExpression(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(cacheNames = "likename" ,allEntries=true)
allEntries=true
開啟全匹配
cacheNames
填寫 模糊刪除的name
感謝各位的閱讀,以上就是“Springboot Cache @CacheEvict無法模糊刪除怎么辦”的內容了,經過本文的學習后,相信大家對Springboot Cache @CacheEvict無法模糊刪除怎么辦這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。