Android 缓存框架之ManbaCacheManager

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);

每天多努力那么一点点,积少成多

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容