服务器发送给我json对象,expiration和ETAg ..我想Voley在缓存中保存这个对象,并在下一个请求中使用该对象请求到服务器,包括标题中的ETag。如果响应将是304 Not Modified,那么它应该使用缓存资源,如果它是200 OK,它应该使用来自服务器的新资源。
Volley根本不发送请求(如果缓存未过期),或者它已过期,它会发送带有If-None-Match + etag字符串的新请求..并且服务器始终响应200
答案 0 :(得分:0)
如果检测到缓存条目尚未过期,Volley将不会向服务器发出请求。以下是一些代码摘录来证明它:
// 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 {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// 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.
}
}
});
}
您可能希望通过在max-age
或Cache-Control
标头设置Expires
来控制服务器上的资源过期,以便在客户端调整缓存。