淺析Java中的Caffeine緩存源碼
1. Caffenie的核心類圖
2. Caffenie緩存的分類
Caffenie從同步-異步、手工加載-自動(dòng)加載、有界-無界三個(gè)維度,可以分為8中緩存類型。
3. LocalCacheFactory緩存工廠
com.github.benmanes.caffeine.cache.LocalCacheFactory#newBoundedLocalCache (只針對(duì)有界隊(duì)列)
static <K, V> BoundedLocalCache<K, V> newBoundedLocalCache(Caffeine<K, V> builder, @Nullable CacheLoader<? super K, V> cacheLoader, boolean async) { StringBuilder sb = new StringBuilder("com.github.benmanes.caffeine.cache."); if (builder.isStrongKeys()) { sb.append('S'); } else { sb.append('W'); } if (builder.isStrongValues()) { sb.append('S'); } else { sb.append('I'); } if (builder.removalListener != null) { sb.append('L'); } if (builder.isRecordingStats()) { sb.append('S'); } if (builder.evicts()) { sb.append('M'); if (builder.isWeighted()) { sb.append('W'); } else { sb.append('S'); } } if (builder.expiresAfterAccess() || builder.expiresVariable()) { sb.append('A'); } if (builder.expiresAfterWrite()) { sb.append('W'); } if (builder.refreshes()) { sb.append('R'); } try { Class<?> clazz = LocalCacheFactory.class.getClassLoader().loadClass(sb.toString()); Constructor<?> ctor = clazz.getDeclaredConstructor(Caffeine.class, CacheLoader.class, boolean.class); @SuppressWarnings("unchecked") BoundedLocalCache<K, V> factory = (BoundedLocalCache<K, V>) ctor.newInstance(builder, cacheLoader, async); return factory; } catch (ReflectiveOperationException e) { throw new IllegalStateException(sb.toString(), e); } }
4. 緩存維護(hù)
com.github.benmanes.caffeine.cache.BoundedLocalCache#maintenance
/** * Performs the pending maintenance work and sets the state flags during processing to avoid * excess scheduling attempts. The read buffer, write buffer, and reference queues are * drained, followed by expiration, and size-based eviction. * * @param task an additional pending task to run, or {@code null} if not present */ @GuardedBy("evictionLock") void maintenance(@Nullable Runnable task) { lazySetDrainStatus(PROCESSING_TO_IDLE); try { drainReadBuffer(); drainWriteBuffer(); if (task != null) { task.run(); } drainKeyReferences(); drainValueReferences(); expireEntries(); evictEntries(); climb(); } finally { if ((drainStatus() != PROCESSING_TO_IDLE) || !casDrainStatus(PROCESSING_TO_IDLE, IDLE)) { lazySetDrainStatus(REQUIRED); } } }
到此這篇關(guān)于淺析Java中的Caffeine緩存源碼的文章就介紹到這了,更多相關(guān)Caffeine緩存源碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線程池FutureTask實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java線程池FutureTask實(shí)現(xiàn)原理詳解,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02MyBatis?Mapper.XML?標(biāo)簽使用小結(jié)
在MyBatis中,通過resultMap可以解決字段名和屬性名不一致的問題,對(duì)于復(fù)雜的查詢,引用實(shí)體或使用<sql>標(biāo)簽可以定義復(fù)用的SQL片段,提高代碼的可讀性和編碼效率,使用這些高級(jí)映射和動(dòng)態(tài)SQL技巧,可以有效地處理復(fù)雜的數(shù)據(jù)庫交互場(chǎng)景2024-10-10SpringCloud中的斷路器(Hystrix)和斷路器監(jiān)控(Dashboard)
本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器指標(biāo)看板(Dashboard)的相關(guān)使用知識(shí),需要的朋友可以參考下2019-06-06關(guān)于@OnetoMany關(guān)系映射的排序問題,使用注解@OrderBy
這篇文章主要介紹了關(guān)于@OnetoMany關(guān)系映射的排序問題,使用注解@OrderBy,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語法之后的練習(xí)項(xiàng)目,通過這個(gè)小項(xiàng)目的練習(xí),對(duì)Java中的類和對(duì)象,抽象類和接口等進(jìn)行熟悉理解??旄S小編一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Spring BeanPostProcessor(后置處理器)的用法
這篇文章主要介紹了Spring BeanPostProcessor(后置處理器)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解
這篇文章主要介紹了Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03深入了解Java語言中的并發(fā)性選項(xiàng)有何不同
這篇文章主要介紹了深入了解Java語言中的并發(fā)性選項(xiàng)有何不同,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06