日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

扩展spring之ext-spring-cache

發布時間:2023/12/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 扩展spring之ext-spring-cache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ext-spring-cache擴展@Cacheable緩存注解

  • ext-spring-cache擴展@Cacheable緩存注解
    • 介紹
    • 前置條件
    • 功能特性
    • 相關配置項
    • 使用說明

介紹

ext-spring-cache的 @ExtCacheable 是對spring的 @Cacheable 的擴展, @ExtCacheable 功能更豐富、用起來更舒心

前置條件

  • spring-boot
  • redis
  • jdk8+

功能特性

  • @ExtCacheable支持指定RedisTemplate操作緩存(對比:@Cacheable不支持指定RedisTemplate)
  • @ExtCacheable支持對具體的key設置過期時間(對比:@Cacheable只支持對命名空間級別設置過期時間)
  • @ExtCacheable支持redis緩存、支持caffeine緩存、支持多級緩存(對比:@Cacheable不全支持)
  • @ExtCacheable對@Cacheable保持兼容
  • 相關配置項

    提示:可詳見com.ideaaedi.extspringcache.constant.ExtCacheConfigPlaceholder

    配置項默認值說明
    ext.spring.cache.print-bannertrue控制是否打印ext-spring-cache banner
    ext.spring.cache.redis-caffeine.caffeine-as-first-cache是否以caffeine作為一級緩存
    true: caffeine作為一級緩存,redis作為二級緩存
    false: redis作為一級緩存,caffeine作為二級緩存
    ext.spring.cache.redis-caffeine.value-back-filltrue(若一級緩存沒數據,二級緩存有數據), 是否回填二級緩存的數據至一級緩存
    ext.spring.cache.use-default-cache-manager-if-misstrue當沒有ext管理器可以被總管理器ExtCacheManager管理時,管理器ExtCacheManager是否管理默認的CacheManager
    ext.spring.cache.redis.response-spring-contexttruetrue: 默認對spring-context中的配置作出響應。 即:在spring-context中若存在相應配置或相關bean,那么會影響所有的Redis。此時,可通過在使用@Redis注解時,顯示的指定相關配置來覆蓋 spring-context中的配置
    false: 默認不對spring-context中的配置作出響應。即:不管spring-context是否存在相應配置或相關bean,都不會影響Redis。此時,在使用@Redis注解時,顯示的指定相關配置依然有效
    ext.spring.cache.caffeine.response-spring-contexttruetrue: 默認對spring-context中的配置作出響應。 即:在spring-context中若存在相應配置或相關bean,那么會影響所有的Caffeine。此時,可通過在使用@Caffeine注解時,顯示的指定相關配置來覆蓋 spring-context中的配置
    false: 默認不對spring-context中的配置作出響應。即:不管spring-context是否存在相應配置或相關bean,都不會影響Caffeine。此時,在使用@Caffeine注解時,顯示的指定相關配置依然有效
    ext.spring.cache.redis-caffeine.response-spring-contexttruetrue: 默認對spring-context中的配置作出響應。 即:在spring-context中若存在相應配置或相關bean,那么會影響所有的Redis、Caffeine。此時,可通過在使用@Redis、@Caffeine注解時,顯示的指定相關配置來覆蓋 spring-context中的配置
    false: 默認不對spring-context中的配置作出響應。即:不管spring-context是否存在相應配置或相關bean,都不會影響Redis、Caffeine。此時,在使用@Redis、@Caffeine注解時,顯示的指定相關配置依然有效
    ………………

    使用說明

    • 第一步:引入依賴

      <dependency><groupId>com.idea-aedi</groupId><artifactId>ext-spring-cache</artifactId><version>{選擇與spring-boot版本號相近的版本}</version> </dependency>
    • 第二步:使用@EnableExtCache注解啟用ext-spring-cache功能

      import com.ideaaedi.extspringcache.EnableExtCache; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableExtCache @SpringBootApplication public class AppApplication {public static void main(String[] args) {SpringApplication.run(AppApplication.class, args);} }
    • 第三步:使用@ExtCacheable注解即可

      • 示例一

        @GetMapping("/demo") @ExtCacheable(cacheNames = "cache-demo", key = "'jd' + #param",caffeine = @Caffeine(expireTime = 300, maximumSize = 3, expireStrategy = CaffeineExpireStrategyEnum.EXPIRE_AFTER_ACCESS) ) public Object two(String param) {System.err.println("進two了\t" + param);return param + ThreadLocalRandom.current().nextInt(100); }
      • 示例二

        @GetMapping("/demo") @ExtCacheable(cacheNames = "cache-demo", key = "'jd' + #param",redis = @Redis(useRedisTemplate = "redisTemplate",expireTime = 100,timeUnit = ChronoUnit.MINUTES,expireStrategy = RedisExpireStrategyEnum.AUTO) ) public Object two(String param) {System.err.println("進two了\t" + param);return param + ThreadLocalRandom.current().nextInt(100); }
      • 示例三

        @GetMapping("/demo") @ExtCacheable(cacheNames = "cache-demo", key = "'jd' + #param",redis = @Redis(useRedisTemplate = "redisTemplate", expireTime = 600),caffeine = @Caffeine(expireTime = 300, maximumSize = 3) ) public Object two(String param) {System.err.println("進two了\t" + param);return param + ThreadLocalRandom.current().nextInt(100); }

    ^_^ 本文已經被收錄進《程序員成長筆記》 ,筆者JustryDeng

    總結

    以上是生活随笔為你收集整理的扩展spring之ext-spring-cache的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。