发现一个好用的 spring 项目缓存注解

26次阅读

共计 2482 个字符,预计需要花费 7 分钟才能阅读完成。

tutu-cache 是为了解决 SpringCache 缓存注解不够灵活的问题而做的 SpringAop 项目。使用 tutu-cache 注解来代替 @Cacheable 和 @CacheEvict 等注解

开源地址:https://github.com/trifolium-x/tutu-cache

文档地址:tutu-cache doc

快速开始

  1. 在 springBoot 中的使用

    • 引入 jar 依赖包

      
      
      
        
      
            co.tunan.tucache
      
            tucache-spring-boot-starter
      
            1.0.4.RELEASE
      
        
      
        
      
        
      
            org.springframework.boot
      
            spring-boot-starter-data-redis
      
        
      
      
      
        
      
      

使用 tu-cache

  1. 使用 tu-cache 对 service 中的方法返回的数据进行缓存

    
    @TuCache("test_service:getList")
    
    public List getList(){return Arrays.asList("tu","nan");
    
    }
    
    
  2. 使用 tu-cache 删除缓存中的数据

    
    @TuCacheClear("test_service:getList")
    
    public void delList(){}
    
    
  3. @TuCache 参数

    • String key() default "" 缓存的字符串格式 key, 支持 spEl 表达式 (使用 #{} 包裹 spEl 表达式),默认值为方法签名

    • long expire() default -1 缓存的过期时间,单位 (秒), 默认永不过期. ( 在 1.0.4.RELEASE 以上版本中建议使用 timeout)

    • boolean resetExpire() default false 每次获取数据是否重置过期时间.

    • TimeUnit timeUnit() default TimeUnit.SECONDS 缓存的时间单位.

    • String condition() default "true" 扩展的条件过滤,值为 spEl 表达式 (直接编写表达式不需要使用 #{} 方式声明为 spEl)

    • 样例:

      
      @TuCache(key="test_service:getList:#{#endStr}", timeout = 10, timeUnit=TimeUnit.SECONDS)
      
      public List getList(String endStr){return Arrays.asList("tu","nan",endStr);
      
      }
      
      // 如果需要当前对象的的方法
      
      @TuCache(key="test_service:getList:#{#this.endStr()}", timeout = 120)
      
      public List getList(){return Arrays.asList("tu","nan",endStr());
      
      }
      
      // 使用 springBean, (使用安全访问符号?.,可以规避 null 错误,具体用法请查看 spEl 表达式)
      
      @TuCache(key="test_service:getList:#{@springBean.endStr()}", timeout = 120)
      
      public List springBeanGetList(){return Arrays.asList("tu","nan",springBean.endStr());
      
      }
      
      // 使用 condition, 当 name 的长度>=5 时进行缓存
      
      @TuCache(key="test_service:getList:#{#name}", condition="#name.length()>= 5")
      
      public List springBeanGetList(String name){return Arrays.asList("tu","nan",name);
      
      }
      
      public String endStr(){return "end";}
      
      
  4. @TuCacheClear 参数

    • String[] key() default {} 删除的 key 数组,支持 spEl 表达式 (使用 #{} 包裹 spEl 表达式)

    • String[] keys() default {} 模糊删除的缓存 key 数组, 支持 spEl 表达式 (使用 #{} 包裹 spEl 表达式), 对应 redis 中deleteKeys(“test_service:”)

    • boolean async() default false 是否异步删除,无需等待删除的结果

    • String condition() default "true" 扩展的条件过滤,值为 spEl 表达式 (直接编写表达式不需要使用 #{} 方式声明为 spEl)

    • 样例:

      
      @TuCacheClear(key={"test_service:itemDetail:#{#id}"})
      
      public void deleteItem(Long id){
      
      }
      
      // 如果需要调用本地的方法
      
      @TuCacheClear(keys={"test_service:itemList:","test_service:itemDetail:#{#id}"}, async = true)
      
      public void deleteItem(Long id){
      
      }
      
      
    • 注意 key 和 keys 的区别

  5. condition 的用法

    • condition 要求 spEL 返回一个 boolean 类型的值,例如:

      • condition = “#param.startsWith(‘a’)”

      • condition = “false”

个性化设置

  • tutu-cache 默认提供了 RedisTuCacheService, 如果用户使用的缓存是 redis 并配置了 redisTemplate 的 bean 则自动使用该默认缓存服务。

  • 用户使用其他缓存,则需要自定义 TuCacheService,实现该接口并注入到 TuCacheBean 中

  • 在 SpringBoot 中在 Configure 类中配置相应的 bean 自动使用自定义的 bean

  • 如果用户需要每个缓存前面添加同意的 keyPrefix,TuCacheBean 的 prefixKey 参数

  • springBoot 中配置

    
    tucache:
    
      enabled: true
    
      cache-type: redis
    
      profiles:
    
        cache-prefix: "my_tu_key_test:"
    
        # ...
    
    
正文完
 0