首頁>技術>

jetcache

簡介github地址;jetcache 的優勢在於比spring cache 提供更加強大的註解操作,並提供兩級快取,本地快取和遠端快取(redis)以及非同步cacheAPI操作,以及其他的TTL等

使用springboot中使用

pom

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

配置檔案

jetcache: areaInCacheName: false #是否加入到cachename中作為字首 statIntervalMinutes: 15 #統計間隔時間 remote: default: #區域area 名 type: redis.lettuce #快取型別 keyPrefix: test # 全域性key字首 keyConvertor: fastjson # key序列化# valueEncoder: bean:jsonEncoder #value序列化,預設java序列化# valueDecoder: bean:jsonDecoder readFrom: master #從那些節點讀資料,master,masterPreferred,slave,slavePreferred,nearest expireAfterWriteInMillis: 1000 # 過期時間,單位毫秒 #mode: MasterSlave(主從模式) #叢集模式 uri: - redis://172.0.0.1:6379 local: default: type: caffeine # 本地快取使用什麼方式,其他還有linkedhashmap limit: 100 # 預設100

另外配置類上加註解

// 開啟在方法上使用快取註解@EnableMethodCache(basePackages = "com.xiaodu.jetcache")// 開啟使用註解方式建立cache@EnableCreateCacheAnnotation

註解說明

@cached作用在方法上,類 必須為一個springbean, key 使用spel表示式

@Cached(area = "default", name = "test.", key = "'str'", expire = 30, localExpire =3, cacheType = CacheType.REMOTE, timeUnit = TimeUnit.SECONDS, localLimit = 5, /*serialPolicy = ""*/cacheNullValue = true) public Object getstr() { System.out.println("str..."); return "str..."; }

@CacheUpdate作用在方法上,類 必須為一個springbean, key 和value 使用spel表示式

@CacheUpdate(name = "test.", key = "'str'" ,value = "#result") public Object getstrupdate() { return "update"; }

@CacheRefresh

// 每隔10秒鐘 就重新整理下快取 @Cached(area = "default", name = "test.", key = "'str'", expire = 3, localExpire =3, cacheType = CacheType.REMOTE, timeUnit = TimeUnit.SECONDS, localLimit = 5, /*serialPolicy = ""*/cacheNullValue = true) @CacheRefresh(refresh = 10) public Object getstr() { System.out.println("str..."); return "str..."; }

@CachePenetrationProtect新增快取穿透保護 是jvm級別程序級別的的不是 分散式的;只保護本服務併發只有一個獲取鎖執行,其他等待

@Cached(area = "default", name = "test.", key = "'str'", expire = 3, localExpire =3, cacheType = CacheType.REMOTE, timeUnit = TimeUnit.SECONDS, localLimit = 5) @CachePenetrationProtect public Object getstr() { System.out.println("str..."); return "str..."; }

@CacheInvalidate(name = "test.", key = "'str'") public void delCache() { System.out.println("delCache name = test. , key = str"); }

@createCache使用

新增@EnableCreateCacheAnnotation開啟,createCache的屬性和cached註解的屬性大致一樣

常用api操作

// 直接在springbean屬性上新增, @CreateCache(area = "default", name = "test.createCache.", expire = 3, cacheType = CacheType.BOTH) private Cache<String, Object> cache; // 操作 cahce的所有方法操作, 大寫的是非同步的,小寫的是同步的,不過小寫的沒返回值的 也是非同步的;底層還是呼叫的非同步操作方法,有返回值的也是呼叫的非同步操作方法 不過在獲取返回值的時候阻塞了 public void testCreateCache() { // 新增快取 cache.put("key1", "value2"); // 新增快取, 並設定超時時間, 若沒設定超時時間則以註解上的設定為準 cache.put("key2", "value2", 10, TimeUnit.SECONDS); // 如果key對應的value不存在,則新增快取value cache.putIfAbsent("key1", "newValue2"); // 如果key對應的value不存在,則透過function 獲取,並關聯key,新增到快取 cache.computeIfAbsent("key1", e -> "computeValue"); /* 以上put 新增快取的操作都是非同步的 */ // 同步獲取快取 Object key1 = cache.get("key1"); // 非同步獲取 CacheGetResult<Object> key11 = cache.GET("key1"); // 刪除快取 boolean key2 = cache.remove("key2"); // 非同步刪除快取, isSuccess()會阻塞獲取結果 boolean key21 = cache.REMOVE("key2").isSuccess(); // CompletableFuture 操作;cache的非同步操作可以像CompletableFuture一樣,進行非同步非同步操作 cache.GET("key1").future() .thenComposeAsync(e -> CompletableFuture.runAsync(() -> { System.out.println(e.getResultCode()); })) .thenAccept(e -> { System.out.println("cache thenaccept"); }); // 分散式鎖操作, 這個需要lock.close() 並且需要判斷lock是否為null try (AutoReleaseLock keyLock = cache.tryLock("keyLock", 10, TimeUnit.SECONDS)) { if (keyLock != null) { System.out.println("lock"); } } // 分散式鎖操作, 上述操作的最佳化版本 cache.tryLockAndRun("keyLock", 10,TimeUnit.SECONDS, () -> { System.out.println("aaa"); }); // umwrap 可以包裝為原始類,進行快取操作, 具體能轉換為什麼型別,請參考實現;注意泛型 RedisStringCommands<byte[], byte[]> unwrap = cache.unwrap(RedisClusterCommands.class); // 分佈中叢集之間只有一個節點獲取到鎖執行,其他服務獲取不到鎖不再競爭,此時可以在reids中提供了set(k,v,nx,ex) 命令 可以實現鎖操作 jetcache中使用 cache.putIfAbsent(k, v) // 自增等操作,jetcache暫時沒做實現 可以使用unwrap 包裝後使用 RedisStringCommands<byte[], byte[]> unwrap = cache.unwrap(RedisClusterCommands.class); Long incr = unwrap.incr("key1".getBytes()); }

以上是在springboot專案中使用 redis-lettuce, 也可以使用 jedis ,不過還是推薦使用 redis-lettuce

jetcache - jedis

簡單寫下吧,基本上和redis-lettuce一致pom

<dependency> <groupId>com.alicp.jetcache</groupId> <artifactId>jetcache-starter-redis</artifactId> <version>2.6.0</version> </dependency>

配置檔案// 基本配置也一樣,jedis 多了一個 pool的配置

jetcache: remote: default: type: redis keyConvertor: fastjson poolConfig: minIdle: 1 maxIdle: 2 maxTotal: 3 host: 172.0.0.1 port: 6379 local: default: type: linkedhashmap

其他看 官網咖;springboot 環境下的jedis支援

以上是jetcache的簡單使用,入手簡單,若不是在springboot環境下 或者 使用原生的 請參照

本文連結:

https://blog.csdn.net/xiaodujava/article/details/112506246

15
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Python影象處理-模糊和銳化