springBoot 自定义缓存注解

1.说明

因为原生的@Cacheable 注解无法声明缓存时间,所以在这里可以自定义一个缓存注解。在声明缓存注解的过程中使用了redis,因为在上一篇文章中已经说明了Spring Boot如何整合redis组件了,所以在此就不做过多的说明了。

2.步骤

1.声明@RedisCache注解

/**
* @description: redis缓存注解
* @author: xiaYZ
* @createDate: 2022/5/24
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisCache {
    //缓存名称
    String value() default "";
    
    //缓存的键名
    String[] key() default {};
    
    //过期时间,默认1小时,单位秒
    int expiredTime() default 3600;
}

2.针对@RedisCache注解进行说明和预处理

/**
 * @author 10412
 */
@Component
@Aspect
public class RedisCacheAspect {

    /**
     * redis操作工具类
     */
    @Resource
    private RedisUtil redisUtil;

    /**
     * redission分布式锁
     */
    @Resource
    private RedissonClient redissonClient;


    @Around("@annotation(com.fwy.common.annotation.redis.RedisCache)")
    public Object cacheAroundAdvice(ProceedingJoinPoint point) throws  Throwable {


        // 获得连接点参数
        Object[] args = point.getArgs();

        // 声明一个对象Object,为方法的返回结果
        Object result = null;
        // 通过反射获得原始方法信息
        MethodSignature signature  =  (MethodSignature)point.getSignature();
        Type returnType = signature.getMethod().getGenericReturnType();
        RedisCache redisCache = signature.getMethod().getAnnotation(RedisCache.class);

        //获取缓存名称
        String key = redisCache.value();
        //获取缓存key
        String[] keys = redisCache.key();
        //获取缓存时间单位s
        int expireTime = redisCache.expiredTime();
        // 根据注解拼接缓存key
        if (keys.length != 0) {
            key += ":" + StringUtils.join(keys,":");
        }
        if(args.length != 0){
            key += ":" + StringUtils.join(args,":");
        }


        // 缓存代码
        result = cacheHit(returnType, key);

        // 表示缓存不为空,则直接返回数据
        if (result != null){
            return result;
        }

        // 使用redisson获得分布式锁
        RLock lock = redissonClient.getLock(key + ":lock");
        Properties prop = new Properties();
        try {
            // 成功拿到分布式锁的,可以查询db
            boolean b = lock.tryLock(100, 10000, TimeUnit.SECONDS);
            if(b){
                // 执行连接点方法,查询db
                result = point.proceed(args);
                // 如果查询不到数据,将空对象放入缓存,防止缓存穿透
                if(result == null){
                    redisUtil.set(key, JSON.toJSONString(new Object()),expireTime);
                    return null;
                }
                // 查询到数据后同步缓存返回结果
                redisUtil.set(key, JSON.toJSONString(result),expireTime);
                // 返回结果给原始方法
                return result;
            }else {
                // 如果没有拿到分布式锁,那么说明已经有人查数据库了,当前执行的线程直接取缓存里面拿其他线程已经存入的数据就行了
                // 等待其他线程放入数据
                Thread.sleep(1000);
                return cacheHit(returnType,key);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            if (lock.isLocked()){
                lock.unlock();
            }
        }
        // 返回原方法需要的的结果
        return result;
    }

    /***
     * 查询缓存中的key
     * @param returnType
     * @param key
     * @return
     */
    private Object cacheHit(Type returnType, String key) {
        Object result = null;
        String cache = (String)redisUtil.get(key);
        if(StringUtils.isNotBlank(cache)){
            result = JSON.parseObject(cache, returnType);
        }
        return result;
    }
}

3.redis序列化处理

@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //序列号key value
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        //解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        //设置默认缓存过期时间
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                // 默认没有特殊指定的,,1天
                .entryTtl(Duration.ofHours(1))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }
}

4.redis缓存工具类

5.使用

//value缓存键值,expiredTime缓存时间  
@Override
    @RedisCache(value = "questionBank",expiredTime = 60*60*24*7)
    public List<QuestionBank> findAllQuestionBank() {
        List<QuestionBank> allQuestionBank = studyDao.findAllQuestionBank();
        allQuestionBank.forEach(question ->{
            question.setOptions(question.getOptions().replaceAll("\n",""));
        });
        return allQuestionBank;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于SpringBoot自定义Redis注解AOP的问题,我可以为您提供一些基本的介绍和示例代码。 Redis作为一种高性能的缓存和数据存储解决方案,被广泛应用于各种应用程序中。在SpringBoot应用程序中,使用Redis通常需要编写大量的重复代码,比如获取Redis连接、执行Redis命令、释放Redis连接等。这些重复代码不仅增加了开发和维护的难度,还影响了应用程序的性能。而AOP作为一种切面编程的技术,可以很好地解决这些问题。 下面是一个简单的示例代码,演示如何通过自定义注解实现对Redis操作的AOP处理: 首先,定义一个自定义注解: ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RedisCacheable { String key() default ""; long expire() default 0; } ``` 然后,在需要被拦截的方法上添加该注解: ```java @Component public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; @RedisCacheable(key = "myKey", expire = 60) public String getValue() { return redisTemplate.opsForValue().get("myKey"); } } ``` 接下来,使用AspectJ的@Aspect注解定义一个切面类,并在该类中定义一个切点,用于匹配被@RedisCacheable注解的方法: ```java @Aspect @Component public class RedisAspect { @Autowired private RedisTemplate<String, String> redisTemplate; @Pointcut("@annotation(com.example.demo.annotation.RedisCacheable)") public void redisCacheablePointcut() {} @Around("redisCacheablePointcut()") public Object aroundRedisCacheable(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); RedisCacheable redisCacheable = method.getAnnotation(RedisCacheable.class); String key = redisCacheable.key(); long expire = redisCacheable.expire(); String value = redisTemplate.opsForValue().get(key); if (value != null) { return value; } Object result = joinPoint.proceed(); if (result != null) { redisTemplate.opsForValue().set(key, result.toString()); if (expire > 0) { redisTemplate.expire(key, expire, TimeUnit.SECONDS); } } return result; } } ``` 在该切面类中,使用@Around注解定义一个环绕通知,在该通知中,首先获取被拦截方法上的@RedisCacheable注解,然后根据注解中的key值从Redis中获取数据。如果Redis中已经存在该数据,则直接返回;否则,执行被拦截方法,并将结果存储到Redis缓存中。 最后,启动SpringBoot应用程序,调用RedisService的getValue方法,就可以看到输出结果: ```java // 第一次调用,从数据库中获取数据,并将数据存入Redis缓存中 getValue... // 第二次调用,直接从Redis中获取数据 getValue... ``` 以上就是一个简单的SpringBoot自定义Redis注解AOP的示例。通过使用自定义注解和AOP技术,可以更加方便地实现对Redis缓存的操作,并提高应用程序的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值