我正在尝试通过记录 NSURLCache 周围的内存统计信息来确认 URL 缓存。
我已经扩展了基础 NSURLCache 如下:
@implementation MarksCache : NSURLCache
- (NSCachedURLResponse *) cachedResponseForRequest:(NSURLRequest *)request;
{
NSString *myString = [request.URL absoluteString];
DDLogVerbose(@"URL CACHE GET : PRE : %@ %d %d %d %d", myString,
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
NSCachedURLResponse *resp = [super cachedResponseForRequest:request];
//NSURLResponse *rp = [resp response];
//NSString *rpdesc = [rp description];
//NSData *data = [resp data];
//NSString *respdesc = [resp description];
//NSString *selfdesc = [super description];
DDLogVerbose(@"URL CACHE GET : POST : %@ %d %d %d %d", myString,
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
return resp;
}
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request
{
NSString *myString = [request.URL absoluteString];
DDLogVerbose(@"URL CACHE SET : PRE : %@ %d %d %d %d", myString,
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
[super cachedResponseForRequest:request];
DDLogVerbose(@"URL CACHE SET : POST : %d %d %d %d",
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
}
@end
我还有执行以下操作的按钮代码:
- (IBAction)onClearButtonPress:(id) sender{
DDLogVerbose(@"TestViewController onClearButtonPress : START : PRE : %d %d %d %d",
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
[NSURLCache sharedURLCache].removeAllCachedResponses;
DDLogVerbose(@"TestViewController onClearButtonPress : START : POST : %d %d %d %d",
[NSURLCache sharedURLCache].currentDiskUsage ,
[NSURLCache sharedURLCache].currentMemoryUsage,
[NSURLCache sharedURLCache].diskCapacity,
[NSURLCache sharedURLCache].memoryCapacity);
}
当我单击视图上的一些测试按钮以触发 URLConnection 事件时,我看到使用正确的 URL 调用这些方法。但是,缓存内存和磁盘使用统计信息永远不会改变。
来自日志的示例
URL CACHE SET : PRE : https://<domain.com>/resources/images/bg/bg-blue.png 1970176 0 10000000 512000
URL CACHE SET : POST : https://<domain.com>/resources/images/bg/bg-blue.png 1970176 0 10000000 512000
URL CACHE GET : PRE : https://<domain.com>/resources/images/bg/bg-footer.png 1970176 0 10000000 512000
URL CACHE GET : POST : https://<domain.com>/resources/images/bg/bg-footer.png 1970176 0 10000000 512000
TestViewController onClearButtonPress : START : PRE : 1970176 0 10000000 512000
TestViewController onClearButtonPress : EXIT : POST : 1970176 0 10000000 512000
URL CACHE SET : PRE : https://<domain.com>/resources/images/bg/bg-footer.png 1970176 0 10000000 512000
URL CACHE SET : POST : https://<domain.com>/resources/images/bg/bg-footer.png 1970176 0 10000000 512000
这让我相信:
1 - 我做错了什么,没有正确观察缓存行为
2 - 这个缓存并不是真正的缓存
任何想法将不胜感激。