当前位置:网站首页>Android内存优化之磁盘缓存
Android内存优化之磁盘缓存
2022-07-20 10:57:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
前言:
在上一篇文章中介绍了内存缓存,内存缓存的长处就是非常快。可是它又有缺点:
- 空间小,内存缓存不可能非常大;
- 内存紧张时可能被清除;
- 在应用退出时就会消失,做不到离线。
基于以上的缺点有时候又须要第二种缓存,那就是磁盘缓存。大家应该都用过新闻client,非常多都有离线功能,功能的实现就是磁盘缓存。
DiskLruCache:
在Android中用到的磁盘缓存大多都是基于DiskLruCache实现的,详细怎么使用呢?
- 创建一个磁盘缓存对象:
public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize);
open()方法接收四个參数。第一个參数是数据的缓存文件地址,第二个參数是当前应用程序的版本,第三个參数是同一个key能够相应多少个缓存文件。一般都是传1,第四个參数是最多能够缓存多少字节的数据,10M?
- 获取缓存路径:
// Creates a unique subdirectory of the designated app cache directory. Tries to use external
// but if not mounted, falls back on internal storage.
//创建磁盘缓存文件。首选sdcard,假设sdcard没有挂载或者没有sdcard则获取应用默认的cache文件夹
public static File getDiskCacheDir(Context context, String uniqueName) {
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
// otherwise use internal cache dir
final String cachePath =
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
!isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
context.getCacheDir().getPath();
return new File(cachePath + File.separator + uniqueName);
}
- 获取软件版本:
public int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
- 完整的代码例如以下:
DiskLruCache mDiskLruCache = null;
try {
File cacheDir = getDiskCacheDir(context, "thumbnails");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
- 详细怎么使用上面创建的磁盘缓存例如以下:
//加入缓存
public void addBitmapToCache(String key, Bitmap bitmap) {
// Add to memory cache as before,把缓存放到内存缓存中
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
// Also add to disk cache,把缓存放入磁盘缓存
synchronized (mDiskCacheLock) {
if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
mDiskLruCache.put(key, bitmap);
}
}
}
//获取缓存
public Bitmap getBitmapFromDiskCache(String key) {
synchronized (mDiskCacheLock) {
// Wait while disk cache is started from background thread
while (mDiskCacheStarting) {
try {
mDiskCacheLock.wait();
} catch (InterruptedException e) {}
}
if (mDiskLruCache != null) {
return mDiskLruCache.get(key);
}
}
return null;
}
总结:
以上是磁盘缓存的创建和用法。在实际操作中内存缓存和磁盘缓存是配合起来使用的。一般先从内存缓存中读取数据,假设没有再从磁盘缓存中读取。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/108572.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
(Note)CIE色度图
experiment....
NC | 城环所朱永官团队-海洋塑料圈在氮素地球化学循环过程中的潜势
第一章 第四节:Pycharm的安装
[jenkins]-流水线循环输出text parameter中的每一行
MySQL数据库执行SQL查询语句时,底层实现原理(超详细)
第二章 第一节:基础数据类型详解
Section 5 of Chapter 1: importance of notes
Section 10 of Chapter 1: conditional judgment if
Excel-vba quick start (VIII. Cell objects - common cell operations)
随机推荐
让你事半功倍的JS utils工具函数
【Go开源宝藏】基于 Golang 语法的性能调优技巧(数组的遍历)
NC | 城环所朱永官团队-海洋塑料圈在氮素地球化学循环过程中的潜势
Rearrange characters of leetcode simple question to form target string
《网络安全测试实验室搭建指南》—第1章1.5节关键术语
[the problem has been solved] - Jenkins traditional packaging and delivery process optimization
基于MCU通用GUI大盘点
mysql进阶(十三)命令行导出导入数据库
亮点抢先看!2022 开放原子全球开源峰会定于 7 月 25-29 日在北京举办
苏州见!“云智技术论坛-工业专场”举办在即
出现线上bug,测试人能做些什么?
Jenkins pipeline downloads the code to the specified workspace
在 Excel 内使用 ODBC 消费 SAP ABAP CDS view
Function introduction
[problem handled] - helm prompt kubernetes configuration file is group readable
Every time the Epson invoice printer continuously prints invoices, the page jumps with a blank in the middle
Tear the ORM container object relational mapping framework (JDBC container)
第一章 第四节:Pycharm的安装
[the problem has been solved] - Jenkins login exsi host without secret
你必须知道的4种 Redis 集群方案及优缺点对比