35
35
static NSString *const kAFURLCacheInfoFileName = @" cacheInfo.plist" ;
36
36
static NSString *const kAFURLCacheInfoAccessesKey = @" accesses" ;
37
37
static NSString *const kAFURLCacheInfoSizesKey = @" sizes" ;
38
+ static NSString *const kAFURLCacheInfoURLsKey = @" URLs" ;
38
39
static float const kAFURLCacheLastModFraction = 0 .1f ; // 10% since Last-Modified suggested by RFC2616 section 13.2.4
39
40
static float const kAFURLCacheDefault = 3600 .0f ; // Default cache expiration delay if none defined (1 hour)
40
41
@@ -419,7 +420,7 @@ + (NSDate *)expirationDateFromHeaders:(NSDictionary *)headers withStatusCode:(NS
419
420
// Define "now" based on the request
420
421
NSString *date = [headers objectForKey: @" Date" ];
421
422
// If no Date: header, define now from local clock
422
- NSDate *now = date ? [SDURLCache dateFromHttpDateString: date] : [NSDate date ];
423
+ NSDate *now = date ? [[ self class ] dateFromHttpDateString: date] : [NSDate date ];
423
424
424
425
// Look at info from the Cache-Control: max-age=n header
425
426
NSString *cacheControl = [[headers objectForKey: @" Cache-Control" ] lowercaseString ];
@@ -447,7 +448,7 @@ + (NSDate *)expirationDateFromHeaders:(NSDictionary *)headers withStatusCode:(NS
447
448
NSString *expires = [headers objectForKey: @" Expires" ];
448
449
if (expires) {
449
450
NSTimeInterval expirationInterval = 0 ;
450
- NSDate *expirationDate = [SDURLCache dateFromHttpDateString: expires];
451
+ NSDate *expirationDate = [[ self class ] dateFromHttpDateString: expires];
451
452
if (expirationDate) {
452
453
expirationInterval = [expirationDate timeIntervalSinceDate: now];
453
454
}
@@ -470,7 +471,7 @@ + (NSDate *)expirationDateFromHeaders:(NSDictionary *)headers withStatusCode:(NS
470
471
NSString *lastModified = [headers objectForKey: @" Last-Modified" ];
471
472
if (lastModified) {
472
473
NSTimeInterval age = 0 ;
473
- NSDate *lastModifiedDate = [SDURLCache dateFromHttpDateString: lastModified];
474
+ NSDate *lastModifiedDate = [[ self class ] dateFromHttpDateString: lastModified];
474
475
if (lastModifiedDate) {
475
476
// Define the age of the document by comparing the Date header with the Last-Modified header
476
477
age = [now timeIntervalSinceDate: lastModifiedDate];
@@ -491,6 +492,9 @@ - (NSMutableDictionary *)diskCacheInfo {
491
492
_diskCacheInfo = [[NSMutableDictionary alloc ] initWithObjectsAndKeys:
492
493
[NSMutableDictionary dictionary ], kAFURLCacheInfoAccessesKey ,
493
494
[NSMutableDictionary dictionary ], kAFURLCacheInfoSizesKey ,
495
+ #if SDURLCACHE_DEBUG
496
+ [NSMutableDictionary dictionary ], kAFURLCacheInfoURLsKey ,
497
+ #endif
494
498
nil ];
495
499
}
496
500
_diskCacheInfoDirty = NO ;
@@ -541,12 +545,18 @@ - (void)removeCachedResponseForCachedKeys:(NSArray *)cacheKeys {
541
545
542
546
NSMutableDictionary *accesses = [self .diskCacheInfo objectForKey: kAFURLCacheInfoAccessesKey ];
543
547
NSMutableDictionary *sizes = [self .diskCacheInfo objectForKey: kAFURLCacheInfoSizesKey ];
548
+ #if SDURLCACHE_DEBUG
549
+ NSMutableDictionary *urls = [self .diskCacheInfo objectForKey: kAFURLCacheInfoURLsKey ];
550
+ #endif
544
551
NSFileManager *fileManager = [[NSFileManager alloc ] init ];
545
552
546
553
while ((cacheKey = [enumerator nextObject ])) {
547
554
NSUInteger cacheItemSize = [[sizes objectForKey: cacheKey] unsignedIntegerValue ];
548
555
[accesses removeObjectForKey: cacheKey];
549
556
[sizes removeObjectForKey: cacheKey];
557
+ #if SDURLCACHE_DEBUG
558
+ [urls removeObjectForKey: cacheKey];
559
+ #endif
550
560
[fileManager removeItemAtPath: [_diskCachePath stringByAppendingPathComponent: cacheKey] error: NULL ];
551
561
552
562
_diskCacheUsage -= cacheItemSize;
@@ -583,7 +593,7 @@ - (void)balanceDiskUsage {
583
593
584
594
585
595
- (void )storeRequestToDisk : (NSURLRequest *)request response : (NSCachedURLResponse *)cachedResponse {
586
- NSString *cacheKey = [SDURLCache cacheKeyForURL: request.URL];
596
+ NSString *cacheKey = [[ self class ] cacheKeyForURL: request.URL];
587
597
NSString *cacheFilePath = [_diskCachePath stringByAppendingPathComponent: cacheKey];
588
598
589
599
[self createDiskCachePath ];
@@ -606,6 +616,9 @@ - (void)storeRequestToDisk:(NSURLRequest *)request response:(NSCachedURLResponse
606
616
// Update cache info for the stored item
607
617
[(NSMutableDictionary *)[self .diskCacheInfo objectForKey: kAFURLCacheInfoAccessesKey ] setObject: [NSDate date ] forKey: cacheKey];
608
618
[(NSMutableDictionary *)[self .diskCacheInfo objectForKey: kAFURLCacheInfoSizesKey ] setObject: cacheItemSize forKey: cacheKey];
619
+ #if SDURLCACHE_DEBUG
620
+ [(NSMutableDictionary *)[self .diskCacheInfo objectForKey: kAFURLCacheInfoURLsKey ] setObject: request.URL.absoluteString forKey: cacheKey];
621
+ #endif
609
622
610
623
[self saveCacheInfo ];
611
624
@@ -643,6 +656,7 @@ + (NSString *)defaultCachePath {
643
656
- (id )initWithMemoryCapacity : (NSUInteger )memoryCapacity diskCapacity : (NSUInteger )diskCapacity diskPath : (NSString *)path {
644
657
if ((self = [super initWithMemoryCapacity: memoryCapacity diskCapacity: diskCapacity diskPath: path])) {
645
658
self.minCacheInterval = kAFURLCacheInfoDefaultMinCacheInterval ;
659
+ self.shouldRespectCacheControlHeaders = YES ;
646
660
self.diskCachePath = path;
647
661
self.ignoreMemoryOnlyStoragePolicy = NO ;
648
662
}
@@ -651,7 +665,7 @@ - (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger
651
665
}
652
666
653
667
- (void )storeCachedResponse : (NSCachedURLResponse *)cachedResponse forRequest : (NSURLRequest *)request {
654
- request = [SDURLCache canonicalRequestForRequest: request];
668
+ request = [[ self class ] canonicalRequestForRequest: request];
655
669
656
670
if (!_allowCachingResponsesToNonCachedRequests &&
657
671
(request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData
@@ -668,15 +682,19 @@ - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NS
668
682
NSURLCacheStoragePolicy storagePolicy = cachedResponse.storagePolicy ;
669
683
if ((storagePolicy == NSURLCacheStorageAllowed || (storagePolicy == NSURLCacheStorageAllowedInMemoryOnly && _ignoreMemoryOnlyStoragePolicy))
670
684
&& [cachedResponse.response isKindOfClass: [NSHTTPURLResponse self ]]
671
- && cachedResponse.data .length < self.diskCapacity ) {
672
- NSDictionary *headers = [(NSHTTPURLResponse *)cachedResponse.response allHeaderFields ];
673
- // RFC 2616 section 13.3.4 says clients MUST use Etag in any cache-conditional request if provided by server
674
- if (![headers objectForKey: @" Etag" ]) {
675
- NSDate *expirationDate = [SDURLCache expirationDateFromHeaders: headers
676
- withStatusCode: ((NSHTTPURLResponse *)cachedResponse.response).statusCode];
677
- if (!expirationDate || [expirationDate timeIntervalSinceNow ] - _minCacheInterval <= 0 ) {
678
- // This response is not cacheable, headers said
679
- return ;
685
+ && cachedResponse.data .length < self.diskCapacity )
686
+ {
687
+ if (self.shouldRespectCacheControlHeaders )
688
+ {
689
+ NSDictionary *headers = [(NSHTTPURLResponse *)cachedResponse.response allHeaderFields ];
690
+ // RFC 2616 section 13.3.4 says clients MUST use Etag in any cache-conditional request if provided by server
691
+ if (![headers objectForKey: @" Etag" ]) {
692
+ NSDate *expirationDate = [[self class ] expirationDateFromHeaders: headers
693
+ withStatusCode: ((NSHTTPURLResponse *)cachedResponse.response).statusCode];
694
+ if (!expirationDate || [expirationDate timeIntervalSinceNow ] - _minCacheInterval <= 0 ) {
695
+ // This response is not cacheable, headers said
696
+ return ;
697
+ }
680
698
}
681
699
}
682
700
@@ -687,14 +705,14 @@ - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NS
687
705
}
688
706
689
707
- (NSCachedURLResponse *)cachedResponseForRequest : (NSURLRequest *)request {
690
- request = [SDURLCache canonicalRequestForRequest: request];
708
+ request = [[ self class ] canonicalRequestForRequest: request];
691
709
692
710
NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest: request];
693
711
if (memoryResponse) {
694
712
return memoryResponse;
695
713
}
696
714
697
- NSString *cacheKey = [SDURLCache cacheKeyForURL: request.URL];
715
+ NSString *cacheKey = [[ self class ] cacheKeyForURL: request.URL];
698
716
699
717
// NOTE: We don't handle expiration here as even staled cache data is necessary for NSURLConnection to handle cache revalidation.
700
718
// Staled cache data is also needed for cachePolicies which force the use of the cache.
@@ -739,10 +757,10 @@ - (NSUInteger)currentDiskUsage {
739
757
}
740
758
741
759
- (void )removeCachedResponseForRequest : (NSURLRequest *)request {
742
- request = [SDURLCache canonicalRequestForRequest: request];
760
+ request = [[ self class ] canonicalRequestForRequest: request];
743
761
744
762
[super removeCachedResponseForRequest: request];
745
- [self removeCachedResponseForCachedKeys: [NSArray arrayWithObject: [SDURLCache cacheKeyForURL: request.URL]]];
763
+ [self removeCachedResponseForCachedKeys: [NSArray arrayWithObject: [[ self class ] cacheKeyForURL: request.URL]]];
746
764
[self saveCacheInfo ];
747
765
}
748
766
@@ -761,12 +779,12 @@ - (void)removeAllCachedResponsesInMemory {
761
779
762
780
- (BOOL )isCached : (NSURL *)url {
763
781
NSURLRequest *request = [NSURLRequest requestWithURL: url];
764
- request = [SDURLCache canonicalRequestForRequest: request];
782
+ request = [[ self class ] canonicalRequestForRequest: request];
765
783
766
784
if ([super cachedResponseForRequest: request]) {
767
785
return YES ;
768
786
}
769
- NSString *cacheKey = [SDURLCache cacheKeyForURL: url];
787
+ NSString *cacheKey = [[ self class ] cacheKeyForURL: url];
770
788
NSString *cacheFile = [_diskCachePath stringByAppendingPathComponent: cacheKey];
771
789
772
790
BOOL isCached = [[[NSFileManager alloc ] init ] fileExistsAtPath: cacheFile];
@@ -787,6 +805,7 @@ - (void)dealloc {
787
805
@synthesize minCacheInterval = _minCacheInterval;
788
806
@synthesize ignoreMemoryOnlyStoragePolicy = _ignoreMemoryOnlyStoragePolicy;
789
807
@synthesize allowCachingResponsesToNonCachedRequests = _allowCachingResponsesToNonCachedRequests;
808
+ @synthesize shouldRespectCacheControlHeaders = _shouldRespectCacheControlHeaders;
790
809
@synthesize diskCachePath = _diskCachePath;
791
810
@synthesize diskCacheInfo = _diskCacheInfo;
792
811
0 commit comments