Android的凌空 - 覆盖缓存超时JSON请求 [英] Android volley - Override Cache Timeout for JSON request

查看:241
本文介绍了Android的凌空 - 覆盖缓存超时JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从服务器缓存JSON请求,但是,它们不正确地使用Cache-Control头(其中包括)(在过去的一切到期)。我要重写它,以便调用缓存说,3个小时,不管什么样的服务器请求。那可能吗?该文档排球是稀缺的。

解决方案

您可以继承的JsonObjectRequest类并覆盖parseNetworkResponse。你会发现在调用HttpHeaderParser.parseCacheHeaders - 这是一个良好的开端:]只是包装这个调用或更换,并提供自己的虚拟缓存头配置对象[你的专属客户方缓存时间]以Response.success

在我的实现,它看起来是这样的:

parseNetworkResponse

 返回Response.success(有效载荷,enforceClientCaching(HttpHeaderParser.parseCacheHeaders(响应),响应));
 

enforceClientCaching 相关成员为

 受保护的静态最终诠释defaultClientCacheExpiry = 1000 * 60 * 60; //毫秒; =1小时

保护Cache.Entry enforceClientCaching(Cache.Entry项,NetworkResponse响应){
    如果(getClientCacheExpiry()== NULL)返回入口;

    长今= System.currentTimeMillis的();

    如果(入口== NULL){
        进入=新Cache.Entry();
        entry.data = response.data;
        entry.etag = response.headers.get(ETag的);
        entry.softTtl =现在+ getClientCacheExpiry();
        entry.ttl = entry.softTtl;
        entry.serverDate现在=;
        entry.responseHeaders = response.headers;
    }否则如果(entry.isExpired()){
        entry.softTtl =现在+ getClientCacheExpiry();
        entry.ttl = entry.softTtl;
    }

    返回入境;
}

保护的整数getClientCacheExpiry(){
    返回defaultClientCacheExpiry;
}
 

它处理两种情况:

  • 在没有缓存的头被设置
  • 在服务器高速缓存条目表示过期的项目

因此​​,如果服务器开始,今后发送正确的缓存头与到期时,它仍然可以工作。

I'm trying to cache JSON requests from a server, however, they are incorrectly using the Cache-Control header, amongst others (everything expires in the past). I want to override it so that calls are cached for say, 3 hours, regardless of what the server requests. Is that possible? The documentation for Volley is Scarce.

解决方案

You might subclass the JsonObjectRequest class and overwrite parseNetworkResponse. You will notice the call to HttpHeaderParser.parseCacheHeaders - it's a good place to start :] just wrap this call or replace it and provide your own dummy Cache header configuration object [with your proprietary clientside cache time] to Response.success.

In my implementation it looks like this:

parseNetworkResponse

return Response.success(payload, enforceClientCaching(HttpHeaderParser.parseCacheHeaders(response), response));

with enforceClientCaching related members being

protected static final int defaultClientCacheExpiry = 1000 * 60 * 60; // milliseconds; = 1 hour

protected Cache.Entry enforceClientCaching(Cache.Entry entry, NetworkResponse response) {
    if (getClientCacheExpiry() == null) return entry;

    long now = System.currentTimeMillis();

    if (entry == null) {
        entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = response.headers.get("ETag");
        entry.softTtl = now + getClientCacheExpiry();
        entry.ttl = entry.softTtl;
        entry.serverDate = now;
        entry.responseHeaders = response.headers;
    } else if (entry.isExpired()) {
        entry.softTtl = now + getClientCacheExpiry();
        entry.ttl = entry.softTtl;
    }

    return entry;
}

protected Integer getClientCacheExpiry() {
    return defaultClientCacheExpiry;
}

It handles 2 cases:

  • no Cache headers were set
  • Server cache entry indicates expired item

So if the server starts sending correct cache headers with expiry in the future, it will still work.

这篇关于Android的凌空 - 覆盖缓存超时JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆