JetCache設(shè)計原理淺析
1、目錄
2、JetCache介紹
3、如何設(shè)計一個緩存組件?
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,可自定義轉(zhuǎn)換器函數(shù)
expireAfterWriteInMillis: 600000
expireAfterAccessInMillis: 300000
remote:
default:
type: redis.lettuce # 支持的類型:redis、redis.lettuce
keyPrefix: '系統(tǒng)簡稱:所屬名字:'
keyConvertor: fastjson
valueEncoder: java # 支持的類型:kryo、java,可自定義編碼器
valueDecoder: java # 支持的類型:kryo、java,可自定義解碼器
expireAfterWriteInMillis: 3600000
#readFrom: slavePreferred # 優(yōu)先從Slave節(jié)點中讀取
uri: redis-sentinel://host1:26379,host2:26379,host3:26379/?sentinelMasterId=mymaster # 哨兵模式
#uri: redis://127.0.0.1:6379/ # 單節(jié)點模式
#mode: masterslave # 設(shè)置為主從模式
#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:抽象類,緩存接口的繼承者,提供基本實現(xiàn),具體實現(xiàn)交由不同的子類
- LinkedHashMapCache:基于LinkedHashMap設(shè)計的簡易內(nèi)存緩存
- CaffeineCache:基于Caffeine工具設(shè)計的內(nèi)存緩存
- RedisCache:Redis實現(xiàn),使用Jedis客戶端
- RedisLettuceCache:Redis實現(xiàn),使用Lettuce客戶端
- MultiLevelCache:兩級緩存,用于封裝EmbeddedCache(本地緩存)和ExternalCache(遠程緩存)
- RefreshCache:基于裝飾器模式Decorator,提供自動刷新功能
- LazyInitCache:用于@CreateCache注解創(chuàng)建的緩存實例,依賴于Spring
JetCache源碼入口
@EbableMethodCache -> JetCacheInterceptor
JetCacheAutoConfiguration
緩存get/put基本實現(xiàn)
Cache->AbstractCache->AbstractEmbeddedCache->LinkedHashMapCache
get()->GET()->do_GET()->map.get()
獲取/存放 數(shù)據(jù) -> 構(gòu)建CacheResult -> 數(shù)據(jù)統(tǒng)計CacheState -> 緩存監(jiān)控CacheMonitor
緩存過期時間的實現(xiàn)
被動過期:
主動過期 Cleaner:
緩存自動刷新的實現(xiàn)
RefreshCache
防止緩存擊穿的實現(xiàn)
@CachePenetrationProtect -> AbstractCache.computeIfAbsentImpl() -> synchronizedLoad();
參考:
浙公網(wǎng)安備 33010602011771號