安卓开发文件缓存方法的具体实现

本文介绍了一个用于管理缓存并优化网络请求的方法。通过设置不同网络环境下的缓存有效期,实现数据的有效利用和节省网络流量。同时,提供了一个自定义缓存解码字符串的函数,确保缓存数据的正确性和格式一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ConfigCache { 
  2     private static final String TAG = ConfigCache.class.getName(); 
  3   
  4     public static final int CONFIG_CACHE_MOBILE_TIMEOUT  = 3600000;  //1 hour 
  5     public static final int CONFIG_CACHE_WIFI_TIMEOUT    = 300000;   //5 minute 
  6   
  7     public static String getUrlCache(String url) { 
  8         if (url == null) { 
  9             return null
 10         } 
 11   
 12         String result = null
 13         File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url)); 
 14         if (file.exists() && file.isFile()) { 
 15             long expiredTime = System.currentTimeMillis() - file.lastModified(); 
 16             Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime/60000 + "min"); 
 17             //1. in case the system time is incorrect (the time is turn back long ago) 
 18             //2. when the network is invalid, you can only read the cache 
 19             if (AppApplication.mNetWorkState != NetworkUtils.NETWORN_NONE && expiredTime < 0) { 
 20                 return null
 21             } 
 22             if(AppApplication.mNetWorkState == NetworkUtils.NETWORN_WIFI 
 23                    && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) { 
 24                 return null
 25             } else if (AppApplication.mNetWorkState == NetworkUtils.NETWORN_MOBILE 
 26                    && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) { 
 27                 return null
 28             } 
 29             try { 
 30                 result = FileUtils.readTextFile(file); 
 31             } catch (IOException e) { 
 32                 e.printStackTrace(); 
 33             } 
 34         } 
 35         return result; 
 36     } 
 37   
 38     public static void setUrlCache(String data, String url) { 
 39         File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url)); 
 40         try { 
 41             //创建缓存数据到磁盘,就是创建文件 
 42             FileUtils.writeTextFile(file, data); 
 43         } catch (IOException e) { 
 44             Log.d(TAG, "write " + file.getAbsolutePath() + " data failed!"); 
 45             e.printStackTrace(); 
 46         } 
 47     } 
 48   
 49     public static String getCacheDecodeString(String url) { 
 50         //1. 处理特殊字符 
 51         //2. 去除后缀名带来的文件浏览器的视图凌乱(特别是图片更需要如此类似处理,否则有的手机打开图库,全是我们的缓存图片) 
 52         if (url != null) { 
 53             return url.replaceAll("[.:/,%?&=]""+").replaceAll("[+]+""+"); 
 54         } 
 55         return null
 56     } 
 57 } 
 58 
 59

    上面的方法如何调用,参考代码如下:

  1 void getConfig(){ 
  2         //首先尝试读取缓存 
  3         String cacheConfigString = ConfigCache.getUrlCache(CONFIG_URL); 
  4         //根据结果判定是读取缓存,还是重新读取 
  5         if (cacheConfigString != null) { 
  6             showConfig(cacheConfigString); 
  7         } else { 
  8             //如果缓存结果是空,说明需要重新加载 
  9             //缓存为空的原因可能是1.无缓存;2. 缓存过期;3.读取缓存出错 
 10             AsyncHttpClient client = new AsyncHttpClient(); 
 11             client.get(CONFIG_URL, new AsyncHttpResponseHandler(){ 
 12                 @Override 
 13                 public void onSuccess(String result){ 
 14                     //成功下载,则保存到本地作为后面缓存文件 
 15                     ConfigCache.setUrlCache(result,  CONFIG_URL); 
 16                     //后面可以是UI更新,仅供参考 
 17                     showConfig(result); 
 18                 } 
 19                 @Override 
 20                 public void onFailure(Throwable arg0) { 
 21                     //根据失败原因,考虑是显示加载失败,还是再读取缓存 
 22                 } 
 23             }); 
 24         } 
 25     } 
 26 
 27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值