SpringBoot Cache 深入

這上一篇文章中我們熟悉了SpringBoot Cache的基本使用,接下來我們看下它的執(zhí)行流程

  1. CacheAutoConfiguration 自動裝配類

    根據(jù)圖中標(biāo)注,看到它引用了CachingConfigurationSelector這個類

a2.png
 靜態(tài)內(nèi)部類,實現(xiàn)了 selectImports()這個方法 ,這個方法用于添加配置類

 通過debugger觀察 imports[]數(shù)組,在控制臺中看到 SimpleCacheConfiguration類的配置生效
     static class CacheConfigurationImportSelector implements ImportSelector {

     @Override
     public String[] selectImports(AnnotationMetadata importingClassMetadata) {
         CacheType[] types = CacheType.values();
         String[] imports = new String[types.length];
         for (int i = 0; i < types.length; i++) {
             imports[i] = CacheConfigurations.getConfigurationClass(types[i]);
         }
         return imports;
     }

 }
  1. SimpleCacheConfiguration 配置類

    創(chuàng)建ConcurrentMapCacheManager對象 ,給容器注冊了CacheManage=>ConcurrentMapCacheManager

    @Bean
    public ConcurrentMapCacheManager cacheManager() {
     ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
     List<String> cacheNames = this.cacheProperties.getCacheNames();
     if (!cacheNames.isEmpty()) {
         cacheManager.setCacheNames(cacheNames);
     }
     return this.customizerInvoker.customize(cacheManager);
    }
    
  1. ConcurrentMapCacheManager 類
    [圖片上傳失敗...(image-2a6db9-1530901912375)]
b2.png

找到getCache()這個方法,根據(jù)方法名就可以知道這是獲取緩存的方法

    @Override
    @Nullable
    public Cache getCache(String name) {
        Cache cache = this.cacheMap.get(name);
        if (cache == null && this.dynamic) {//判斷是否為null
            synchronized (this.cacheMap) {//加鎖
                cache = this.cacheMap.get(name);
                if (cache == null) {
                    cache = createConcurrentMapCache(name);//保存至createConcurrentMapCache
                    this.cacheMap.put(name, cache);
                }
            }
        }
        return cache;
    }

createConcurrentMapCache方法

protected Cache createConcurrentMapCache(String name) {
        SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null);
        return new ConcurrentMapCache(name, new ConcurrentHashMap<>(256),
                isAllowNullValues(), actualSerialization);

    }

回到ConcurrentHashMap類,找到lookup()這個方法 ,這個緩存Key是怎么得到的呢,對這個方法打斷點,看它的調(diào)用棧

@Override
    @Nullable
    protected Object lookup(Object key) {
        return this.store.get(key);
    }

e2.png

進入generatorKey()這個方法


c3.png

找到這個接口,是不是有了熟悉的感覺,這就是自定義主鍵生成策略需要實現(xiàn)的接口


d2.png

致此,整合流程也就走完了,這里增加一點內(nèi)容關(guān)于@Caching
這個注解相當(dāng)于把 @CachePut、 @CacheEvict、@Cacheable這三個注解組合在一起,增加了靈活性,使用與之前類似,就不展開了
f2.png

如理解有誤,請指正

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • JSR107 Java Caching定義了5個核心接口,分別是CachingProvider, CacheMan...
    匆匆歲月閱讀 1,512評論 0 1
  • 本文轉(zhuǎn)自被遺忘的博客 Spring Cache 緩存是實際工作中非常常用的一種提高性能的方法, 我們會在許多場景下...
    quiterr閱讀 1,199評論 0 8
  • 今天讀完了第四章和第五章。早上讀到手不釋卷,打心想眼兒里有點喜歡這個作者,可以看得出他的知識關(guān)聯(lián)整合的能力超強。這...
    劉小麥同學(xué)閱讀 459評論 0 0
  • 估計很多朋友看到這個題目,第一反應(yīng)會不會看錯了!其實沒有看錯,這個故事是我們大人要講給孩子聽的故事!當(dāng)我看到幼兒園...
    Cinko閱讀 9,060評論 0 17

友情鏈接更多精彩內(nèi)容