文件缓存法的具体实现
针对配置文件的缓存,我新建了一个类ConfigCache:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import java.io.File; import java.io.IOException; import android.util.Log; import com.tianxia.app.floworld.AppApplication; import com.tianxia.app.floworld.utils.FileUtils; import com.tianxia.app.floworld.utils.NetworkUtils; public class ConfigCache { private static final String TAG = ConfigCache. class .getName(); public static final int CONFIG_CACHE_MOBILE_TIMEOUT = 3600000 ; //1 hour public static final int CONFIG_CACHE_WIFI_TIMEOUT = 300000 ; //5 minute public static String getUrlCache(String url) { if (url == null ) { return null ; } String result = null ; File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url)); if (file.exists() && file.isFile()) { long expiredTime = System.currentTimeMillis() - file.lastModified(); Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime/ 60000 + "min" ); //1. in case the system time is incorrect (the time is turn back long ago) //2. when the network is invalid, you can only read the cache if (AppApplication.mNetWorkState != NetworkUtils.NETWORN_NONE && expiredTime < 0 ) { return null ; } if (AppApplication.mNetWorkState == NetworkUtils.NETWORN_WIFI && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) { return null ; } else if (AppApplication.mNetWorkState == NetworkUtils.NETWORN_MOBILE && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) { return null ; } try { result = FileUtils.readTextFile(file); } catch (IOException e) { e.printStackTrace(); } } return result; } public static void setUrlCache(String data, String url) { File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url)); try { //创建缓存数据到磁盘,就是创建文件 FileUtils.writeTextFile(file, data); } catch (IOException e) { Log.d(TAG, "write " + file.getAbsolutePath() + " data failed!" ); e.printStackTrace(); } } public static String getCacheDecodeString(String url) { //1. 处理特殊字符 //2. 去除后缀名带来的文件浏览器的视图凌乱(特别是图片更需要如此类似处理,否则有的手机打开图库,全是我们的缓存图片) if (url != null ) { return url.replaceAll( "[.:/,%?&=]" , "+" ).replaceAll( "[+]+" , "+" ); } return null ; } } |
从实现上我们全面考虑了几个细节,注释已经说明,不再赘述。
然后我们调用方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
void getConfig(){ //首先尝试读取缓存 String cacheConfigString = ConfigCache.getUrlCache(CONFIG_URL); //根据结果判定是读取缓存,还是重新读取 if (cacheConfigString != null ) { showConfig(cacheConfigString); } else { //如果缓存结果是空,说明需要重新加载 //缓存为空的原因可能是1.无缓存;2. 缓存过期;3.读取缓存出错 AsyncHttpClient client = new AsyncHttpClient(); client.get(CONFIG_URL, new AsyncHttpResponseHandler(){ @Override public void onSuccess(String result){ //成功下载,则保存到本地作为后面缓存文件 ConfigCache.setUrlCache(result, CONFIG_URL); //后面可以是UI更新,仅供参考 showConfig(result); } @Override public void onFailure(Throwable arg0) { //根据失败原因,考虑是显示加载失败,还是再读取缓存 } }); } } |
这样配置文件既能有效缓存,又能及时更新了,同时支持离线浏览。
posted on 2014-02-26 16:46 clarenceV1 阅读(233) 评论(0) 编辑 收藏 举报