您好,登錄后才能下訂單哦!
這篇文章主要講解了“Springboot Cache @CacheEvict無法模糊刪除怎么辦”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Springboot Cache @CacheEvict無法模糊刪除怎么辦”吧!
用@CacheEvict刪除緩存只能刪除指定key的緩存,有些情況需要根據(jù)前綴刪除所有key的時(shí)候,用@CacheEvict就做不到了,所以我們自定義一個(gè)@CacheRemove來處理根據(jù)前綴模糊刪除所有cache(支持Spring EL表達(dá)式)
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
啟動(dòng)類加上 @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實(shí)現(xiàn)類代碼
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中的調(diào)用代碼
/** * 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無法模糊刪除怎么辦”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Springboot Cache @CacheEvict無法模糊刪除怎么辦這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。