ios - AFNetworking - 请求和图像的缓存时间如何工作?

标签 ios image caching request afnetworking

<分区>

我正在使用 AFNetworking iOS 框架。

  1. 缓存由 HTTP 响应 header 控制?
    • 如果是,它基于 header “Cache-Control”和“Expires”以用于正常请求?
    • 如果不是,缓存是如何工作的?
  2. 您可以自定义缓存时间并在 1 天内修复它,而不考虑 HTTP 响应 header ?
  3. 如果有图片缓存,是存储在磁盘上还是保存在内存中?
  4. 缓存请求在设备没有互联网连接时有效吗?
    • 用户发出请求,但设备显示没有互联网连接,请求被暂停。当设备恢复互联网连接时,您可以忽略相同的请求吗?
  5. 我的应用程序使用数据库离线工作的唯一方法还是可以基于此框架的系统缓存?
  6. 您可以更改请求的超时而无需自定义“NSURLRequest”?

最佳答案

The cache is controlled by the HTTP response header?

是的;这就是 NSURLCache 决定是否缓存数据以及缓存多长时间的方式。

It is based on the headers 'Cache-Control' and 'Expires' for normal requests?

是的,但值得注意的是,如果缓存周期太短,它有时会忽略缓存请求。不幸的是,这不是记录在案的行为。

You can customize the cache time and fixed it in 1 day, regardless of the HTTP response headers?

为了更好地控制缓存,您可以使用 SDURLCache:https://github.com/rs/SDURLCache AFNetworking 在他们的常见问题解答中推荐了这个库。

If there is image cache, it is stored on disk or saved to memory?

对于一般请求,这可由开发人员配置(请参阅 NSURLCache 文档)。

对于UIImageView+AFNetworking,除了提供内存缓存的NSURLCache之外,还使用了NSCache。

The cache requests work when the device has no internet connection?

是的,但是缓存随时可能失效,所以不要依赖这个。

The user makes a request but the device showed no internet connection and the request was suspended. You can ignore the same requests when the device returns to its internet connection? The only way my application would work offline using database or can be based on the system cache of this framework?

您可以在收到 NSURLRequest 后手动检查缓存的响应:

NSMutableURLRequest *request = [myHTTPClient requestWithMethod:@"GET" path:path parameters:parameters];
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (cachedResponse)
{
    // Use response
}

You can change the timeout of requests without having to customize a 'NSURLRequest'?

不是开箱即用的,但您可以覆盖在子类中创建 NSMutableURLRequest 的方法,以便为每个子类更改它。这个方法是requestWithMethod:path:parameters:

关于ios - AFNetworking - 请求和图像的缓存时间如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886169/

相关文章:

ios - 找到一周中依赖于语言环境的第一天

ios - UITableViewController 无声地崩溃而没有错误

ios - 核心数据 swift : Crash : SQLite error code:14, 'unable to open database file'

android - 保存的图像覆盖了 SD 卡 android 中的先前图像

c++ - 多种字体大小的 FreeType 字形指标缓存

ios - iOS SDK 中是否有可供选择的默认背景图片?

java - 使图像遵循帧尺寸

python - mayavi(imshow)中如何绘制彩色图像

c# - 在 ASP.NET 中使用静态字段来缓存大对象是不好的做法吗?

python - 有没有和 sqlite 一样的 nosql 平面文件数据库?