mirror of
https://github.com/BgaSol/sol-cloud
synced 2026-05-24 09:38:21 +00:00
feat(common-base-web): Add cache to clear faces and annotations
This commit is contained in:
parent
be08569228
commit
1c592a57ef
2 changed files with 62 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
package com.bgasol.common.core.base.aspect;
|
||||
|
||||
import com.bgasol.common.core.base.cache.CacheEvictById;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CacheEvictAspect {
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
|
||||
|
||||
@Around("@annotation(cacheEvictById)")
|
||||
public void cacheEvictById(JoinPoint joinPoint, CacheEvictById cacheEvictById) {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
|
||||
// 获取参数名与值
|
||||
EvaluationContext context = new StandardEvaluationContext();
|
||||
String[] paramNames = signature.getParameterNames();
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
context.setVariable(paramNames[i], args[i]);
|
||||
}
|
||||
|
||||
String rawKey = cacheEvictById.key();
|
||||
Expression expression = spelExpressionParser.parseExpression(rawKey);
|
||||
String keyValue = (String) expression.getValue(context);
|
||||
String cacheKey = cacheEvictById.cacheName() + ":" + keyValue;
|
||||
// 删除缓存
|
||||
boolean delete = redissonClient.getBucket(cacheKey).delete();
|
||||
if (delete) {
|
||||
log.info("删除缓存:{}", cacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
cloud/common/common-base-web/src/main/java/com/bgasol/common/core/base/cache/CacheEvictById.java
vendored
Normal file
14
cloud/common/common-base-web/src/main/java/com/bgasol/common/core/base/cache/CacheEvictById.java
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package com.bgasol.common.core.base.cache;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CacheEvictById {
|
||||
String cacheName();
|
||||
|
||||
String key();
|
||||
}
|
||||
Loading…
Reference in a new issue