Android APP 缓存技术笔记1

来源:互联网 发布:c语言数据结构怎么学 编辑:程序博客网 时间:2024/06/04 23:23

0.APP缓存管理技术

0.1 数据法管理缓存

这种方法是在下载完数据文件后,把文件的相关信息如url,路经,下载时间,过期时间等存放到数据库,下次下载的时候根据url先从数据库中查询,如果查询到当前时间并未过期,就根据路径读取本地文件,从而实现缓存的效果。

0.2 文件法管理缓存

使用File.lastModified()方法得到文件的最后修改时间,与当前时间判断是否过期,从而实现缓存效果。

1.Eoe客户端源码中的缓存技术(数据法管理缓存)使用的核心代码及其解析

1.1 主函数分析:

package cn.eoe.app.utils;//工具类包

RequestCacheUtil.java

public class RequestCacheUtil {

//  公有方法

    public static String getRequestContent(Context context, StringRequestUrl,String source_type, String content_type,boolean UseCache)

DBHelper dbHelper = DBHelper.getInstance(context);

String md5 = MD5.encode(RequestUrl);                   

//1.判断SD卡是否可用

if (!CommonUtil.sdCardIsAvailable())/*SD卡不可用 */{

//2. 确定缓存的文件名路径

String cachePath =context.getCacheDir().getAbsolutePath() + "/" + md5;

           returngetCacheRequest(context, RequestUrl, cachePath,source_type,content_type, dbHelper, UseCache);

       } else {//SD卡可用

/* 3.对于API级别8或更高的版本,可以使用getExternalCacheDir()来打开一个File对象,它代表了保存缓存文件的外部存储器目录*/

String imagePath = getExternalCacheDir(context)+ File.separator + md5;

return getCacheRequest(context, RequestUrl, imagePath,source_type,

content_type, dbHelper,UseCache);

       }

}

 

//私有方法

private static String getCacheRequest(Context context, StringrequestUrl,String requestPath, String source_type, String content_type,

           DBHelper dbHelper, boolean useCache) {

       String result = "";

       if (useCache) {//4.使用缓存

//5.先看getStringFromSoftReference软缓存里面有没有缓存这个内容,如果有直接拿取返回上层

           result = getStringFromSoftReference(requestUrl);

           if (!result.equals(null) && !result.equals("")) {

              return result;

           }

//6.如果没有就进入二级缓存(即本地缓存文件),二级缓存的相关信息(url,路经,下载时间,过期时间等)保存在SQLite数据库文件表request_cache

result = getStringFromLocal(requestPath,requestUrl, dbHelper);

           if (!result.equals(null) && !result.equals("")) {

//7.二级缓存数据不为空,则将其放入软缓存

              putStringForSoftReference(requestUrl, result);

              return result;

           }

       }

  //8.不使用缓存 直接从网络获取

       result = getStringFromWeb(context, requestPath,requestUrl,

              source_type, content_type, dbHelper);

       return result;

    }

}

 

1.2其它主要子功能函数:

<001>.getStringFromSoftReference(String requestUrl){

//从保存软引用的HashMap中获取指定requestUrl对应的软引用对象

}

<002>. String getStringFromLocal(String requestPath,

           String requestUrl,DBHelper dbHelper){

//从request_cache表中查询requestUrl对应的记录

//比较当前时间和该记录的时间戳之差,如果过期了则删除requestPath路径对应的缓存文件,如果没有过期则获取requestPath路径对应的缓存文件的内容

}

<003>. String getStringFromWeb(Context context, String requestPath,

String requestUrl, String source_type, String content_type,

DBHelper dbHelper){

//从网络下载指定requestUrl对应的资源内容result

a.更新数据表request_cache

//在request_cache表中查询该requestUrl记录,如果不为空,更新该条记录对应的时间戳,如果为空,在该表中插入该requestUrl对应的缓存记录

b.保存该资源内容到缓存文件中(写入缓存文件)

//删除原来的requestPath文件,将result保存在指定文件requestPath中

C.将资源内容添加到软引用对应的hashMap中(添加到软引用

//键:requestUrl   值:new SoftReference<String>(result)

}

1.3 缓存过期的处理

附源码:

private static String getStringFromLocal(String requestPath,
String requestUrl, DBHelper dbHelper) {
String result = "";
Cursor cursor = getStringFromDB(requestUrl, dbHelper);
if (cursor.moveToFirst()) {
Long timestamp = cursor.getLong(cursor
.getColumnIndex(RequestCacheColumn.Timestamp));

String strContentType = cursor.getString(cursor
.getColumnIndex(RequestCacheColumn.Content_type));
long span = getSpanTimeFromConfigs(strContentType);
long nowTime = System.currentTimeMillis();
if ((nowTime - timestamp) > span * 60 * 1000) {
// 过期
deleteFileFromLocal(requestPath);
} else {
// 没过期
result = getFileFromLocal(requestPath);
}
}
return result;
}


/** 根据类型获取缓存时间 */
private static long getSpanTimeFromConfigs(String str) {
long span = 0;
if (str.equals(Constants.DBContentType.Content_list)) {
span = Configs.Content_ListCacheTime;
} else if (str.equals(Constants.DBContentType.Content_content)) {
span = Configs.Content_ContentCacheTime;
} else if (str.equals(Constants.DBContentType.Discuss)) {
span = Configs.DiscussCacheTime;
} else {
span = Configs.Content_DefaultCacheTime;
}
return span;
}


package cn.eoe.app.config;
public class Configs {
// 分钟  不同资源类型对应的缓存时间不同
public static int Content_ListCacheTime = 5;
public static int Content_ContentCacheTime = 60 * 24 * 3;
public static int ImageCacheTime = 60 * 24 * 15;
public static int Content_DefaultCacheTime = 60 * 24 * 3;
public static int DiscussCacheTime=60;
}


0 0
原创粉丝点击