淺談Glide緩存key的問題
最近項(xiàng)目里面有個地方是在前面用glide加載圖片后,后面再另外一個地方加載相同圖片時沒有復(fù)用glide的緩存,而是自己另外又重新緩存了一套。
查找后發(fā)現(xiàn)問題是glide緩存的key不一致的問題。
從key的生成可以看到和很多參數(shù)有關(guān),逐一排查后,發(fā)現(xiàn)了width和height還有id不一樣。這3個是項(xiàng)目外面?zhèn)鬟M(jìn)來的。
EngineKey key = keyFactory.buildKey(id, signature, width, height, loadProvider.getCacheDecoder(), loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(), transcoder, loadProvider.getSourceEncoder());
key的作用大概是通過下面三步里面去找數(shù)據(jù)
如果都為null,就會進(jìn)入函數(shù)最后邊的開線程去decode(相當(dāng)于緩存沒找到,準(zhǔn)備重新加載數(shù)據(jù)吧)
EngineJob engineJob = engineJobFactory.build(key, isMemoryCacheable); DecodeJob<T, Z, R> decodeJob = new DecodeJob<T, Z, R>(key, width, height, fetcher, loadProvider, transformation, transcoder, diskCacheProvider, diskCacheStrategy, priority); EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority); jobs.put(key, engineJob); engineJob.addCallback(cb); engineJob.start(runnable);
進(jìn)入EngineRunnable的run方法看
resource = decode(); private Resource<?> decode() throws Exception { if (isDecodingFromCache()) { return decodeFromCache(); } else { return decodeFromSource(); } }
其中l(wèi)oadCache還是loadFromSource的條件
private boolean isDecodingFromCache() { return stage == Stage.CACHE; }
默認(rèn)stage會進(jìn)去,走到decodeFromCache(),由于cache里沒有,返回null到run方法里面觸發(fā)加載失敗的回調(diào)
if (resource == null) { onLoadFailed(exception); } else { onLoadComplete(resource); }
在回調(diào)中重新提交一個runnable,改變stage,下一次run執(zhí)行時,stage==source,就不會去loadCache,而是loadSource。(開線程加載大概流程感覺就像是默認(rèn)先去緩存中找,沒找到就重新加載)
private void onLoadFailed(Exception e) { if (isDecodingFromCache()) { stage = Stage.SOURCE; manager.submitForSource(this); } else { manager.onException(e); } }
loadSource會一路走到
private Resource<T> decodeFromSourceData(A data) throws IOException { final Resource<T> decoded; if (diskCacheStrategy.cacheSource()) { decoded = cacheAndDecodeSourceData(data); } else { long startTime = LogTime.getLogTime(); decoded = loadProvider.getSourceDecoder().decode(data, width, height); if (Log.isLoggable(TAG, Log.VERBOSE)) { logWithTimeAndKey("Decoded from source", startTime); } }
這里回調(diào)的decode就是項(xiàng)目中自己設(shè)置的sourceDecoder
項(xiàng)目內(nèi)的代碼象征性的打碼:
之前id和寬高傳的不一樣,導(dǎo)致key不一樣,然后Glide加載的時候通過key找不到緩存,最后就又回調(diào)到項(xiàng)目里面的decode那里來了。
改完后,第一次decode完后,后面用緩存就不會再進(jìn)入decode了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法
- Glide4 高效加載圖片的配置詳解
- Glide4.6.1 GlideApp無法生成的問題的解決
- Android Glide 4.0+使用詳解
- Android中Glide獲取圖片Path、Bitmap用法詳解
- Android將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法
- Android添加glide庫報(bào)錯Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解決
- android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽
- Android利用Glide獲取圖片真正的寬高的實(shí)例
- Android中Glide獲取緩存大小并清除緩存圖片
- 導(dǎo)入takephoto庫編譯失敗與glide庫沖突應(yīng)排除依賴
相關(guān)文章
Android Button按鈕點(diǎn)擊背景和文字變化操作
這篇文章主要介紹了Android Button按鈕點(diǎn)擊背景和文字變化操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android App中的多個LinearLayout嵌套布局實(shí)例解析
這篇文章主要介紹了Android App中的多個LinearLayout嵌套布局實(shí)例,利用線性布局來排列按鈕是安卓應(yīng)用布局中的常用做法,需要的朋友可以參考下2016-04-04Android Studio中導(dǎo)入JNI生成的.so庫的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio中導(dǎo)入JNI生成的.so庫的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供實(shí)現(xiàn)方案并提供了實(shí)現(xiàn)的方法,需要的朋友可以參考下2017-07-07Android開發(fā)中下拉刷新如何實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07Android布局加載之LayoutInflater示例詳解
這篇文章主要介紹了Android布局加載之LayoutInflater的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考借鑒價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03Android?獲取手機(jī)已安裝的應(yīng)用列表實(shí)現(xiàn)詳解
這篇文章主要介紹了Android?獲取手機(jī)已安裝的應(yīng)用列表的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09