Permalink
Browse files

Allow forcing caching of non-cache-using requests.

  • Loading branch information...
1 parent 42ad9c7 commit 7cda7703c23379f09febe8ee02700399fe0b87af @bjhomer bjhomer committed Jun 6, 2012
Showing with 15 additions and 3 deletions.
  1. +10 −0 SDURLCache.h
  2. +5 −3 SDURLCache.m
View
10 SDURLCache.h
@@ -38,6 +38,16 @@
*/
@property (nonatomic, assign) BOOL ignoreMemoryOnlyStoragePolicy;
+
+/*
+ * Allow caching responses for a request with a cache policy that ignores the local cache.
+ * Usually, these responses don't need to be cached, because a request that ignores the cache
+ * once is likely to do so every time it is used.
+ *
+ * The default value is NO.
+ */
+@property (nonatomic, assign) BOOL allowCachingResponsesToNonCachedRequests;
+
/*
* Returns a default cache director path to be used at cache initialization. The generated path directory
* will be located in the application's cache directory and thus won't be synced by iTunes.
View
8 SDURLCache.m
@@ -652,9 +652,10 @@ - (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request {
request = [SDURLCache canonicalRequestForRequest:request];
- if (request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData
- || request.cachePolicy == NSURLRequestReloadIgnoringLocalAndRemoteCacheData
- || request.cachePolicy == NSURLRequestReloadIgnoringCacheData) {
+ if (!_allowCachingResponsesToNonCachedRequests &&
+ (request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData
+ || request.cachePolicy == NSURLRequestReloadIgnoringLocalAndRemoteCacheData
+ || request.cachePolicy == NSURLRequestReloadIgnoringCacheData)) {
// When cache is ignored for read, it's a good idea not to store the result as well as this option
// have big chance to be used every times in the future for the same request.
// NOTE: This is a change regarding default URLCache behavior
@@ -784,6 +785,7 @@ - (void)dealloc {
@synthesize minCacheInterval = _minCacheInterval;
@synthesize ignoreMemoryOnlyStoragePolicy = _ignoreMemoryOnlyStoragePolicy;
+@synthesize allowCachingResponsesToNonCachedRequests = _allowCachingResponsesToNonCachedRequests;
@synthesize diskCachePath = _diskCachePath;
@synthesize diskCacheInfo = _diskCacheInfo;

0 comments on commit 7cda770

Please sign in to comment.