문제

I am attempting to confirm URL caching by logging the memory stats around a NSURLCache.

I have extended the base NSURLCache as follows:

@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

I also have button code that executes the following:

- (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);

    }

When I click some test buttons on my view to fire URLConnection events I see these methods being called with the correct URLS. However, the cache memory and disk usage stats NEVER change.

Example from log

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

This makes me believe:

1 - I am doing something wrong, and not correctly observing the cache behavior

2 - This cache is not really caching

Any thoughts would be much appreciated.

도움이 되었습니까?

해결책

I figured this out a while ago and wanted to post answer. I had made my code the delegate for: NSURLConnection - connection:willCacheResponse

That method allows you to override caching behavior before it happens. I has simply placed logging in it to see my Response were cachable, and forgotten to actually do any caching. Once fixed my memory stats logging indeed showed items enter and exiting the cache.

URL Loading System Programming Guide

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top