java - volley中的entry.softTtl和entry.ttl有什么区别?

标签 java android android-volley

在 HttpHeaderParser 类中:

public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;

    long serverDate = 0;
    long serverExpires = 0;
    long softExpire = 0;
    long maxAge = 0;
    boolean hasCacheControl = false;

    String serverEtag = null;
    String headerValue;

    headerValue = headers.get("Date");
    if (headerValue != null) {
        serverDate = parseDateAsEpoch(headerValue);
    }

    headerValue = headers.get("Cache-Control");
    if (headerValue != null) {
        hasCacheControl = true;
        String[] tokens = headerValue.split(",");
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i].trim();
            if (token.equals("no-cache") || token.equals("no-store")) {
                return null;
            } else if (token.startsWith("max-age=")) {
                try {
                    maxAge = Long.parseLong(token.substring(8));
                } catch (Exception e) {
                }
            } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                maxAge = 0;
            }
        }
    }

    headerValue = headers.get("Expires");
    if (headerValue != null) {
        serverExpires = parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    // Cache-Control takes precedence over an Expires header, even if both exist and Expires
    // is more restrictive.
    if (hasCacheControl) {
        softExpire = now + maxAge * 1000;
    } else if (serverDate > 0 && serverExpires >= serverDate) {
        // Default semantic for Expire header in HTTP specification is softExpire.
        softExpire = now + (serverExpires - serverDate);
    }

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = entry.softTtl;
    entry.serverDate = serverDate;
    entry.responseHeaders = headers;

    return entry;
}

entry.softTtl = softExpire;

entry.ttl = entry.softTtl;

这两个变量有相同的值,为什么?

在类 CacheDispatcher 中

@Override
public void run() {
    ...
    ...

    // If it is completely expired, just send it to the network.
    if (entry.isExpired()) {
        request.addMarker("cache-hit-expired");
        request.setCacheEntry(entry);
        mNetworkQueue.put(request);
        continue;
    }

    ...

    if (!entry.refreshNeeded()) {
        // Completely unexpired cache hit. Just deliver the response.
        mDelivery.postResponse(request, response);
    } else {
        ...

        // Post the intermediate response back to the user and have
        // the delivery then forward the request along to the network.
        mDelivery.postResponse(request, response, new Runnable() {
            @Override
            public void run() {
                try {
                    mNetworkQueue.put(request);
                } catch (InterruptedException e) {
                    // Not much we can do about this.
                }
            }
        });
    }

    ...
}

由于值相同,如何区分 entry.isExpired() 和 entry.refreshNeeded() 的转换?

最佳答案

看看这篇文章:Group Google : Volley Users - soft and hard ttl on cache

refreshNeeded() and isExpired() are not exactly the same. One compares to the ttl value and the other to softTtl. This can be used to implement request semantics where you will return a response from cache even if it is "soft" expired, but will then go to the network and refresh, returning a new response if the data has changed.

关于java - volley中的entry.softTtl和entry.ttl有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523435/

相关文章:

android - Volley 库和 HTTPS 请求

java - 如何等到环境是非 headless (headless)的?

java - AES加密/解密java bouncy caSTLe解释?

java - 在 PRG 模式中如何在刷新成功页面上删除 ActionMessage

android - 有没有办法拦截发送到不同传输的Android SMS?

java - 无法从 START_ARRAY 中反序列化 java.lang.String 的实例

android - 使用 Volley 发布请求总是会在带有 Laravel 后端的 Android 中抛出服务器错误

java - 我可以用什么大 double 来模拟无穷大?

java - 在android中使用线程连续将异常记录到文件中

java - 我如何在 Android 的 onResponse 之外使用变量?