2012-11-15 25 views
2

我试图通过记录NSURLCache周围的内存统计信息来确认URL缓存。NSURLCache:说它的缓存,没有,并没有改变内存统计

我已经扩展底座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事件我看到用正确的URLS调用这些方法。但是,高速缓存内存和磁盘使用率统计信息永远不会改变。

从日志实例

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 - 这个缓存是不是真的缓存

任何想法将不胜感激。

回答

1

我刚才想到了这个,想发表回答。 我已将我的代码作为代理人: NSURLConnection - connection:willCacheResponse

该方法允许您在发生高速缓存行为之前覆盖它。我只是简单地将日志记录放入其中,以查看我的响应是否可以缓存,并且忘记实际执行缓存。一旦修复我的内存统计日志确实显示项目进入和退出缓存。

URL Loading System Programming Guide