@Data
@ConfigurationProperties("spring.redis")
public class RedissonProperties {
private String host = "localhost";
private String addresses = "";
private String password = "";
private String port = "6379";
private int timeout = 3000;
private int connectionPoolSize = 64;
private int connectionMinimumIdleSize=10;
private int pingConnectionInterval = 60000;
public static String ADDRESS_PREFIX = "redis://";
}
2. AopCacheConf 组件配置类
public class AopCacheConf {
//需要 redisTemplate redissonClient
@Bean
public CacheService cacheService(StringRedisTemplate stringRedisTemplate,RedissonClient redissonClient)
{
if (StringUtils.isEmpty(redissonClient)){
throw new RuntimeException("请往容器中添加RedissonClient组件");
}
System.out.println("cacheService方法执行了,往容器中添加CacheService组件");
return new CacheServiceImpl(stringRedisTemplate,redissonClient);
}
@Bean
public GmallCacheAspect gmallCacheAspect(CacheService cacheService)
{
System.out.println("GmallCacheAspect方法执行了,往容器中添加GmallCacheAspect组件");
return new GmallCacheAspect(cacheService);
}
}
public class AopCacheSelector implements importSelector {
public String[] selectimports(Annotationmetadata importingClassmetadata) {
//要添加的组件的全限定类名
System.out.println("selectimports方法执行了,往容器中添加AopCacheConf组件");
return new String[]{"com.sgg.aopcache.cache.conf.AopCacheConf"};
}
}
5.切面类
@Slf4j
@Aspect
public class GmallCacheAspect {
//没抢到默认的等待时间
private AtomicLong atomicLong = new AtomicLong(1000);
private CacheService cacheService;
public GmallCacheAspect(CacheService cacheService) {
this.cacheService = cacheService;
}
public GmallCacheAspect() {
}
@Around("@annotation(com.sgg.aopcache.cache.anno.GmalllCache)")
public Object around(ProceedingJoinPoint joinPoint) throws Exception {
//先拿到目标方法执行的参数
Object[] args = joinPoint.getArgs();
//拿到方法声明
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//拿到方法声明上注解
Method method = signature.getMethod();
//获取方法方法的返回值类型
final Type returnType = method.getGenericReturnType();
GmalllCache cacheAnnotation = method.getAnnotation(GmalllCache.class);
//拿到注解上的值 缓存数据的key
String cacheKeyExpr = cacheAnnotation.cacheKey();
String bloomValueExpr = cacheAnnotation.bloomValue();
//Sp EL表达式 解析
String cacheKey = parseSpel(cacheKeyExpr, joinPoint,"cacheKey");
Long bloomValue=null;
if (!StringUtils.isEmpty(bloomValueExpr)){
String bloomVlaueStr = parseSpel(bloomValueExpr, joinPoint,"bloomValue");
bloomValue = Long.valueOf(bloomVlaueStr);
}
while (true){
//从缓冲中拿数据 并根据方法的返回值类型封装数据
Object dataFromCache = cacheService.getDataFromCache(cacheKey, new TypeReference
6.CacheService
CacheService接口
public interface CacheService {
T getDataFromCache(String spelValue, TypeReference typeReference) throws Exception;
RBloomFilter getBloomFilter(String bloomName);
RLock getLock(String lockKey);
void saveCacheData(String cacheKey, Object detail, Type returnType) throws Exception;
}
CacheServiceImpl 实现类
public class CacheServiceImpl implements CacheService {
StringRedisTemplate redisTemplate; //操作redis
RedissonClient redissonClient; //使用redisson
ObjectMapper objectMapper = new ObjectMapper();
public CacheServiceImpl(StringRedisTemplate redisTemplate, RedissonClient redissonClient) {
this.redisTemplate = redisTemplate;
this.redissonClient = redissonClient;
}
public T getDataFromCache(String spelValue, TypeReference returnType) throws Exception {
String json = redisTemplate.opsForValue().get(spelValue);
if (StringUtils.isEmpty(json)) {
return null;
}
T value = objectMapper.readValue(json, returnType);
return value;
}
public RBloomFilter getBloomFilter(String bloomName) {
RBloomFilter