ios - NSURLCache cachedResponseForRequest 不检索缓存数据

标签 ios caching networking nsurlcache

我正在尝试从 NSURLCache 获取之前缓存的信息。使用此代码:

NSString *theCompleteURL = @"http://192.168.1.2:8080/api/endpoint.json";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:theCompleteURL]];

NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (response) {
    NSLog(@"Cached data found");
}
else {
    NSLog(@"Cached data not found");
}

但我总是从方法 cachedResponseForRequest 得到 nil 作为对“response”变量的响应。

我确信数据在缓存中,因为我在我的应用程序的 Cache.db 文件中检查了它,从 cfurl_cache_response 表中得到了这个结果:

sqlite> select * from cfurl_cache_response;
1|0|1875686237|0|http://192.168.1.2:8080/api/endpoint.json|2014-01-09 11:55:17|
sqlite> 

在 ApplicationDelegate 中,NSURLCache 配置为:

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024
                                                  diskCapacity:40 * 1024 * 1024
                                                      diskPath:nil];

[NSURLCache setSharedURLCache:cache];

知道缓存会发生什么吗?

我端点的 header 如下所示:

$ curl -I http://192.168.1.2:8080/api/endpoint.json
HTTP/1.1 200 OK
Content-Length: 1385
Expires:  Thu, 01 Dec 2020 16
00: 00 GMT
Content-Type: application/json;charset=utf-8
ETag:  "3e86-410-3596fbbc"
Cache-Control:  max-age=3600
Connection: keep-alive
Server: thin 1.5.1 codename Straight Razor
$ 

最佳答案

这可以通过与响应关联的 header 来解释,特别是 Cache-controlEtag 字段。另见 here :

13.1.3 Cache-control Mechanisms

The basic cache mechanisms in HTTP/1.1 (server-specified expiration times and validators) are implicit directives to caches. In some cases, a server or client might need to provide explicit directives to the HTTP caches. We use the Cache-Control header for this purpose.

The Cache-Control header allows a client or server to transmit a variety of directives in either requests or responses. These directives typically override the default caching algorithms. As a general rule, if there is any apparent conflict between header values, the most restrictive interpretation is applied (that is, the one that is most likely to preserve semantic transparency).

13.3.2 Entity Tag Cache Validators

The ETag response-header field value, an entity tag, provides for an "opaque" cache validator. This might allow more reliable validation in situations where it is inconvenient to store modification dates, where the one-second resolution of HTTP date values is not sufficient, or where the origin server wishes to avoid certain paradoxes that might arise from the use of modification dates.

你可以找到here Cache-control 和 here 的允许值那些用于 Etag 的。

关于ios - NSURLCache cachedResponseForRequest 不检索缓存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21023396/

相关文章:

ios - iOS UITextView 中的多种超链接颜色

iphone - 带有文本的可 ScrollView 以及在点击特定短语时执行某些操作的能力

ios - Phonegap 3.3.0 无法添加任何插件

sql - 调用 Oracle 函数的 JPA native 查询在同一事务上下文中的不同调用中返回相同的对象

Android 简单 2 路 IM 问题(可运行)

ios - 如何使用 include_media_info 获取元数据?

php - CloudFlare 用于缓存在核心 php 中开发的 REST API 响应

android - 没有可用于离线模式的gradle缓存版本

c# - 我无法通过互联网连接到服务器

c# - 不完整的消息(C# TCP/IP 客户端)