ManbaCacheManager
ManbaCacheManager是Android緩存管理器,分為兩級緩存:內存緩存和外部存儲介質緩存;先取內存數據,沒有再從外部存儲介質中讀取數據。
特點
- 二級緩存
- 內存緩存:采用LruCache實現
- 外部存儲介質緩存:采用DiskLruCache實現
- 默認使用SD卡緩存
- 支持的數據類型:String
- 默認內存緩存優(yōu)先
- 采用單例模式創(chuàng)建緩存管理器,再結合簡單工廠模式,創(chuàng)建存儲模式。
寫緩存流程圖
open=>start: open
close=>end: End
init=>operation: init
Strategy=>condition: MEMORY_FIRST or MEMORY_ONLY ?
memoryOnly=>operation: memoryOnly
memoryFirst=>operation: memory with Disk
open->init->Strategy
Strategy(yes)->memoryFirst->close
Strategy(no)->memoryOnly->close
注:MEMORY_FIRST:內存緩存優(yōu)先,外部存儲緩存為輔;MEMORY_ONLY:只有內存緩存
讀緩存流程圖
open=>start: open
close=>end: End
init=>operation: init
Strategy=>condition: MEMORY_FIRST or MEMORY_ONLY ?
memoryOnly=>operation: memoryOnly
memoryFirst=>operation: memory with Disk
memoryCache=>condition: Yes or No ?
readMemoryCache=>operation: readMemoryCache
readDiskMemoryCache=>operation: readDiskMemoryCache
open->init->Strategy
Strategy(yes)->memoryFirst->memoryCache
Strategy(no)->memoryOnly->close
memoryCache(yes)->readMemoryCache->close
memoryCache(no)->readDiskMemoryCache->close
使用方法
ManbaCacheManager.getInstance(context, ManbaCacheManager.Strategy.MEMORY_FIRST);
//讀數據
mCacheManager.readCache(CacheKey);
//寫數據
mCacheManager.writeCache(CacheKey, data);
//移除數據
mCacheManager.removeMemoryCache(CacheKey);
每天多努力那么一點點,積少成多