ManbaCacheManager
ManbaCacheManager是Android缓存管理器,分为两级缓存:内存缓存和外部存储介质缓存;先取内存数据,没有再从外部存储介质中读取数据。
特点
二级缓存
内存缓存:采用LruCache实现
外部存储介质缓存:采用DiskLruCache实现
默认使用SD卡缓存
支持的数据类型:String
默认内存缓存优先
采用单例模式创建缓存管理器,再结合简单工厂模式,创建存储模式。
写缓存流程图
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:内存缓存优先,外部存储缓存为辅;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);
每天多努力那么一点点,积少成多