【发布时间】:2025-12-10 00:00:02
【问题描述】:
我正在尝试让我的应用使用我的服务器提供的 Last-Modified 标头。
问题是应用程序不断缓存来自服务器的响应,我一次又一次地尝试清除缓存,不允许它缓存响应等,但没有任何工作。我尝试了以下方法:
对于AFNetworking:
- (AFHTTPRequestOperationManager *)manager {
if (!_manager) {
_manager = [AFHTTPRequestOperationManager manager];
_manager.responseSerializer = [AFJSONResponseSerializer serializer];
[_manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}
return _manager;
}
设置NSURLRequestReloadIgnoringLocalCacheData 无效。
我在我的delegate 中试过这个:
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
我什至尝试将它们设置为 0 以消除所有缓存的可能性。还是不行。
我也尝试过如下删除缓存:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] removeCachedResponsesSinceDate:last];
即使这样也没有用。当我从服务器收到新的响应时,它只是再次从缓存中重新加载!还有什么我没有尝试过的对这里有帮助的吗?当我删除服务器响应的 Last-Modified 标头时,一切正常。但这不是正确的解决方案。
我还阅读了以下内容:
http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/
【问题讨论】:
-
我记得在 url 中添加了一个额外的唯一参数(例如时间戳),这会强制重新加载缓存。不确定是否是这样的情况,例如网页缓存而不重新加载,即使它们包含要重新加载的 js .所以基本上我们访问相同的网址,但添加一个 ×tamp=572888166777(类似的东西)
-
可能是服务器端问题?请尝试一次@user3344236 解决方案。
-
我认为这不是服务器端问题——非常适合我的 Android 应用。另外,我使用 Django,它可以自动完成大部分工作。
-
你可能必须使用 NSURLRequestReturnCacheDataElseLoad 模式。
-
NSURLRequestReturnCacheDataElseLoad怎么不使用缓存?我试图阻止它从缓存中加载。
标签: ios afnetworking-2