gpt4 book ai didi

ios - 如何使用 NSURLCache 来缓存由 NSURLProtocol 服务的内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:15 24 4
gpt4 key购买 nike

我已经编写了一个 NSURLProtocol,它将检查出站 http 请求是否针对 URL 到本地路径映射的 plist 并提供本地内容,然后使用 NSURLCache:

缓存它
- (void)startLoading
{
//Could this be why my responses never come out of the cache?
NSURLResponse *response =[[NSURLResponse alloc]initWithURL:self.request.URL
MIMEType:nil expectedContentLength:-1
textEncodingName:nil];

//Get the locally stored data for this request
NSData* data = [[ELALocalPathSubstitutionService singleton] getLocallyStoredDataForRequest:self.request];

//Tell the connection to cache the response
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];

//Have the connection load the data we just fetched
[[self client] URLProtocol:self didLoadData:data];

//Tell the connection to finish up
[[self client] URLProtocolDidFinishLoading:self];
}

我将可以获取本地数据的次数限制为一次。第一次获取它的意图来自 NSBundle,但此后它将使用库存 NSURLCache 来检查它是否应该来自 NSBundle strong>缓存或网络:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
//Check if we have pre-loaded data for that request
ELAPathSubstitution* pathSub = [[ELALocalPathSubstitutionService singleton] pathSubForRequest:request];

//We don't have a mapping for this URL
if (!pathSub)
return NO;

//If it's been fetched too many times, don't handle it
if ([pathSub.timesLocalDataFetched intValue] > 0)
{
//Record that we refused it.
[pathSub addHistoryItem:ELAPathSubstitutionHistoryRefusedByProtocol];
return NO;
}

//Record that we handled it.
[pathSub addHistoryItem:ELAPathSubstitutionHistoryHandledByProtocol];
return YES;
}

遗憾的是,本地数据似乎会进入缓存,但永远不会返回。这是一个日志片段:

History of [https://example.com/image.png]:
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryHandledByProtocol]
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryHandledByProtocol]
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryHandledByProtocol]
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryCacheMiss]
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryDataFetched]
[2014-04-29 18:01:53 +0000] = [ELAPathSubstitutionHistoryAddedToCache]
[2014-04-29 18:02:11 +0000] = [ELAPathSubstitutionHistoryRefusedByProtocol]
[2014-04-29 18:02:11 +0000] = [ELAPathSubstitutionHistoryRefusedByProtocol]

[2014-04-29 18:02:11 +0000] = [ELAPathSubstitutionHistoryCacheMiss]
[2014-04-29 18:02:11 +0000] = [ELAPathSubstitutionHistoryAddedToCache]
[2014-04-29 18:02:50 +0000] = [ELAPathSubstitutionHistoryRefusedByProtocol]
[2014-04-29 18:02:50 +0000] = [ELAPathSubstitutionHistoryCacheHit]

我的期望是,在第一次被协议(protocol)拒绝后,它会导致几次缓存命中,但它总是将其视为未命中,从服务器获取内容,然后我开始获取缓存命中。

我担心的是,我的 NSURLProtocol 子类以一种允许它们被缓存但阻止它们被从缓存中拉出的方式构造它的响应。有什么想法吗?

提前致谢。 :)

最佳答案

与 URL 加载系统的缓存交互是作为 NSURLProtocol 客户端的 NSURLProtocolClient 对象的责任。如果请求使用 NSURLRequestUseProtocolCachePolicy 作为缓存策略,则由协议(protocol)实现应用正确的协议(protocol)特定规则来确定是否应缓存响应。

协议(protocol)实现,在任何适合协议(protocol)的点,调用 URLProtocol:cachedResponseIsValid:在它的客户端上,表明缓存的响应是有效的。然后客户端应该与 URL 加载系统的缓存层交互。

但是,由于系统为我们提供的客户端是私有(private)且不透明的,您可能希望自己动手并在您的协议(protocol)中与系统缓存进行交互。如果你想走那条路,你可以直接使用 NSURLCache。第一步是覆盖协议(protocol)中的 -cachedResponse。如果您仔细阅读文档,默认实现只会根据传递给初始化程序的值设置它。覆盖它以便它访问 shared URL cache (或您自己的私有(private) URL 缓存):

- (NSCachedURLResponse *) cachedResponse {
return [[NSURLCache sharedURLCache] cachedResponseForRequest:[self request]];
}

现在在客户端通常调用cachedResponseIsValid: 的地方,也将NSCachedURLResponse 存储到NSURLCache 中。例如,当您有一组完整的字节和一个响应时:

[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:[self request]];

关于ios - 如何使用 NSURLCache 来缓存由 NSURLProtocol 服务的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23374675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com