ios – NSURLCache cachedResponseForRequest不检索缓存数据

ios – NSURLCache cachedResponseForRequest不检索缓存数据,第1张

概述我试图从NSURLCache获取以前缓存的信息.使用此代码: NSString *theCompleteURL = @"http://192.168.1.2:8080/api/endpoint.json";NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:theCompleteURL]];NSC 我试图从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.

我确信数据是在缓存中,因为我在我的应用程序的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];

关于缓存可能发生什么的任何想法?

我的端点的标题看起来像:

$curl -I http://192.168.1.2:8080/API/endpoint.Jsonhttp/1.1 200 OKContent-Length: 1385Expires:  Thu,01 Dec 2020 1600: 00 GMTContent-Type: application/Json;charset=utf-8ETag:  "3e86-410-3596fbbc"Cache-Control:  max-age=3600Connection: keep-aliveServer: thin 1.5.1 codename Straight Razor$
解决方法 这可以通过与响应关联的标头来解释,特别是Cache-control和Etag字段.另见 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的允许值和0700 Etag的允许值.

总结

以上是内存溢出为你收集整理的ios – NSURLCache cachedResponseForRequest不检索缓存数据全部内容,希望文章能够帮你解决ios – NSURLCache cachedResponseForRequest不检索缓存数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1022186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存