Jetcache+SpringBoot配置


Maven依賴

  • jedis依賴
<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis</artifactId>
    <version>2.6.0</version>
</dependency>

  • lettuce依賴
<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis-lettuce</artifactId>
    <version>2.6.0</version>
</dependency>

yaml配置

參考源碼:

com.alicp.jetcache.autoconfigure.JetCacheProperties

com.alicp.jetcache.autoconfigure.AbstractCacheAutoInit

com.alicp.jetcache.autoconfigure.RedisAutoConfiguration.RedisAutoInit

  • jedis配置
jetcache:
  areaInCacheName: false
  remote:
    default:
      type: redis
      keyConvertor: fastjson
      poolConfig:
        minIdle: 10
        maxIdle: 300
        maxTotal: 300
      password: 123456
      database: 2
      sentinels: localhost:8001,localhost:8002,localhost:8003
      masterName: REDIS
  • lettuce配置
jetcache:
  areaInCacheName: false
  remote:
    default:
      type: redis.lettuce
      keyConvertor: fastjson
      #valueEncoder: kryo
      #valueDecoder: kryo
      poolConfig:
        minIdle: 10
        maxIdle: 300
        maxTotal: 300
      password: 123456
      database: 3
      masterName: REDIS
      uri: redis-sentinel://${jetcache.remote.default.password}@localhost:8001,localhost:8002,localhost:8003/?sentinelMasterId=${jetcache.remote.default.masterName}&database=${jetcache.remote.default.database}

啟動注解

  • @EnableCreateCacheAnnotation
    • 開啟可通過@CreateCache注解創建Cache實例功能
  • @EnableMethodCache(basePackages = "com.xxx.xxx")
    • 開啟可通過@Cached注解創建Cache實例功能,初始化spring aop
/**
 * 啟動類
 */
@SpringBootApplication
@EnableCreateCacheAnnotation
@EnableMethodCache(basePackages = "com.xxx.xxx")
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

緩存注解

//緩存
@Cached(name = CENTER_DATA_CACHE_KEY, expire = 60 * 60 * 24 * 2, cacheNullValue = true)
public DmEffBillYear getData(BillDataReq req) {    
    reture null;
}

//刷新緩存
@CacheUpdate(name = CENTER_DATA_CACHE_KEY, value = "#result")
@Override
public DmEffBillYear freshData(BillDataReq req) {
    return getData(req);
}

Jetcache wiki

Jetcache Jedis

Jetcache lettuce

Jetcache Springboot

JetCache 緩存框架的使用以及源碼分析


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM