1、目錄
2、JetCache介紹
3、如何設計一個緩存組件?
4、SpringCache VS JetCache
5、JetCache基本使用
5.1 JetCache配置信息
jetcache:
statIntervalMinutes: 60
areaInCacheName: false
penetrationProtect: true
enableMethodCache: true
hiddenPackages: com.xxx.xxx,com.xxx.xxx
local:
default:
type: caffeine # 支持的類型:linkedhashmap、caffeine
limit: 100
keyConvertor: fastjson # 支持的類型:fastjson,可自定義轉換器函數
expireAfterWriteInMillis: 600000
expireAfterAccessInMillis: 300000
remote:
default:
type: redis.lettuce # 支持的類型:redis、redis.lettuce
keyPrefix: '系統簡稱:所屬名字:'
keyConvertor: fastjson
valueEncoder: java # 支持的類型:kryo、java,可自定義編碼器
valueDecoder: java # 支持的類型:kryo、java,可自定義解碼器
expireAfterWriteInMillis: 3600000
#readFrom: slavePreferred # 優先從Slave節點中讀取
uri: redis-sentinel://host1:26379,host2:26379,host3:26379/?sentinelMasterId=mymaster # 哨兵模式
#uri: redis://127.0.0.1:6379/ # 單節點模式
#mode: masterslave # 設置爲主從模式
#uri: # 集羣模式
#- redis://127.0.0.1:7000
#- redis://127.0.0.1:7001
#- redis://127.0.0.1:7002
5.2、JetCache使用示例
6、JetCache原理
- Cache:緩存接口,定義基本方法
- AbstractCache:抽象類,緩存接口的繼承者,提供基本實現,具體實現交由不同的子類
- LinkedHashMapCache:基於LinkedHashMap設計的簡易內存緩存
- CaffeineCache:基於Caffeine工具設計的內存緩存
- RedisCache:Redis實現,使用Jedis客戶端
- RedisLettuceCache:Redis實現,使用Lettuce客戶端
- MultiLevelCache:兩級緩存,用於封裝EmbeddedCache(本地緩存)和ExternalCache(遠程緩存)
- RefreshCache:基於裝飾器模式Decorator,提供自動刷新功能
- LazyInitCache:用於@CreateCache註解創建的緩存實例,依賴於Spring
JetCache源碼入口
@EbableMethodCache -> JetCacheInterceptor
JetCacheAutoConfiguration
緩存get/put基本實現
Cache->AbstractCache->AbstractEmbeddedCache->LinkedHashMapCache
get()->GET()->do_GET()->map.get()
獲取/存放 數據 -> 構建CacheResult -> 數據統計CacheState -> 緩存監控CacheMonitor
緩存過期時間的實現
被動過期:
主動過期 Cleaner:
緩存自動刷新的實現
RefreshCache
防止緩存擊穿的實現
@CachePenetrationProtect -> AbstractCache.computeIfAbsentImpl() -> synchronizedLoad();
參考: