Android HttpResponseCache缓存

最近项目选用Picasso作为项目中为数不多的头像加载,不管怎么说源码神马的还是要看一下才用,果然,Picasso使用好简单,尤其是做DiskCache的时候,如果不是用OKHttp作为下载器的话,DiskCache直接委托给了Android的HttpResponseCache去做DiskCache,这有什么问题呢,问题在于我不想做DiskCache还不行了,伸缩性不强,我想针对某种需求做DiskCache比如联系人头像加载,但是网络资源搜索中的头像我就不想做DiskCache,这一点Picasso不像UIL那样可配置,有点跑题了,主角是HttpResponseCache。

Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. This class supports HttpURLConnection and HttpsURLConnection; there is no platform-provided cache for DefaultHttpClient or AndroidHttpClient.

使用缓存可以重复利用结果,减少网络请求,加快响应速度等好处多多,但是这个是在你的项目中使用的是HttpURLConnection and HttpsURLConnection,对于DefaultHttpClient or AndroidHttpClient 不生效,原因很简单的,HttpClient好强大,好难拓展的,在我的一篇博文中已经讲过,Android之后将会专注于Urlconnection,并在Android 5.1 中已经去掉了HttpClient,如何使用呢:

//做这么多就可以了,其他在你每次做请求的时候,UrlConnection会帮你缓存结果的,你可以到具体的目录下查看
//缓存的结果,就是一些header和具体的数据。
protected void onCreate(Bundle savedInstanceState) {
      try {
          File httpCacheDir = new File(context.getCacheDir(), "http");
          long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
          HttpResponseCache.install(httpCacheDir, httpCacheSize);
      } catch (IOException e) {
         Log.i(TAG, "HTTP response cache installation failed:" + e);
      }
}

清除缓存:

protected void onStop() {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
         cache.delete();
    }
}

好,到这里需求来了,对于某个请求我不想用cache怎整:

connection.addRequestProperty("Cache-Control", "no-cache");

好,需求又来了,我想用cache但是又怕服务器的数据已经发生改变了:

connection.addRequestProperty("Cache-Control", "max-age=0");

强制使用缓存的方法:

try {
    connection.addRequestProperty("Cache-Control", "only-if-cached");
    InputStream cached = connection.getInputStream();
      // the resource was cached! show it
    catch (FileNotFoundException e) {
      // the resource was not cached
   }
}

这里呢HttpResponseCache是Android4.0 之后才支持的,4.0以前用什么(TMD 还要兼容4.0以前的版本,怎么不QS,我是大公司,就要有大公司的气魄,兼容)

try {
     File httpCacheDir = new File(context.getCacheDir(), "http");
     long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
     Class.forName("android.net.http.HttpResponseCache")
                 .getMethod("install", File.class, long.class)
                 .invoke(null, httpCacheDir, httpCacheSize);
     } catch (Exception httpResponseCacheNotAvailable) {
         //已经有人做了移植了,在github上输入HttpResponseCache
         //https://github.com/candrews/HttpResponseCache
     }
}

另外附上一些可用的API:

public void close ();
public void flush ();
public CacheResponse get (URI uri, String requestMethod, Map<String, List<String>> requestHeaders);
public int getHitCount ();
public static HttpResponseCache getInstalled ();
public int getNetworkCount ();
public int getRequestCount ();
public static HttpResponseCache install (File directory, long maxSize);
public long maxSize ();
public CacheRequest put (URI uri, URLConnection urlConnection);
public long size ();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值