iOS 网络缓存总结

一、缓存策略:

 1、缓存策略的配置:

缺省缓存策略的存储策略需要服务器的响应配置;

缺省缓存策略的使用需要请求端的配置;

2、缓存策略的缺陷:

移动端比较通用的缓存策略是先使用缓存同时更新本地数据;

缺省的缓存策略更多是同步确认后再使用本地缓存;

这就需要移动端对缓存策略进行定制;一方面把缓存数据从网络层同步到应用层,另一方面执行原有的网络请求和缓存的策略,将数据同步到应用层;

二、缓存方案的关注点:

1、取:发起请求时从缓存中取数据;

2、存:响应回来时将数据存储到缓存;

 三、请求的一致性:

 缓存的存取实际上相当于map的存取;

这个时候请求的一致性就相当重要;需要重载请求的isequal和hash函数;

原因:在一个请求中可能会添加一些随机信息,比如时间戳;但是这个信息不应该影响请求一致性的判断;

而系统的缺省实现可能把这些因素添加了进去。

经过测试,NSURLCache本身就对这个操作进行了处理

Aspects: Error: isEqual: already hooked in NSURLCache. A method can only be hooked once per class hierarchy.

Aspects: Error: hash already hooked in NSURLCache. A method can only be hooked once per class hierarchy.

但是这个可能也不是我们想要的,需要进一步处理。

四、缓存机制的实现方案:

缓存的处理时机:协议层、网络层、应用层。

1、切面或协议代理:

相当于网络层的低一层实现网络缓存;

可能需要禁止掉网络层(系统的)缓存策略;

本质是实现了MITM攻击

//禁止系统缓存

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

  [self setResponse:response];

  [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];  // We cache ourselves.

}

2、使用系统的缓存机制:

1、缓存策略配置;

2、网络层数据的修改

一般来说,如果你需要修改需要缓存的内容,那么你需要新创建一个NSURLCachedResponse对象来被缓存,同时用于下一次的返回。另外返回nil则会阻止缓存行为。如:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection

                      willCacheResponse:(NSCachedURLResponse *)cachedResponse

    {

        NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy];

        NSMutableData *mutableData = [[cachedResponse data] mutableCopy];

        NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;

        // ...

        return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]

                                                        data:mutableData

                                                    userInfo:mutableUserInfo

                                               storagePolicy:storagePolicy];

    }

或者

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection

                  willCacheResponse:(NSCachedURLResponse *)cachedResponse

{

    return nil;

}

3、在应用层实现:

调用网络请求和数据返回时进行缓存管理

4、在网络层禁止原来的缓存策略自己定制:

1)在回调代理层层面禁掉原来的缓存模式,自己实现缓存存储;

2)在请求层面,自己进行缓存管理,获取缓存数据并返回。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

  [self setResponse:response];

  [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];  // We cache ourselves.

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

  [[self client] URLProtocolDidFinishLoading:self];

  NSString *cachePath = [self cachePathForRequest:[self request]];

  RNCachedData *cache = [RNCachedData new];

  [cache setResponse:[self response]];

  [cache setData:[self data]];

  [NSKeyedArchiver archiveRootObject:cache toFile:cachePath];

  [self setConnection:nil];

  [self setData:nil];

  [self setResponse:nil];

}

参考:

https://github.com/rnapier/RNCachingURLProtocol

https://github.com/artifacts/AFCache

原文地址:https://www.cnblogs.com/feng9exe/p/10418586.html