ASIDownloadCache: Added removeCachedDataForURL: (closes gh-180)
Internal tweaks to remove a bit of duplication
Showing
3 changed files
with
53 additions
and
44 deletions
| @@ -58,7 +58,7 @@ typedef enum _ASICacheStoragePolicy { | @@ -58,7 +58,7 @@ typedef enum _ASICacheStoragePolicy { | ||
| 58 | 58 | ||
| 59 | - (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request; | 59 | - (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request; |
| 60 | 60 | ||
| 61 | -// Should Remove cached data for a particular request | 61 | +// Removes cached data for a particular request |
| 62 | - (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; | 62 | - (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; |
| 63 | 63 | ||
| 64 | // Should return YES if the cache considers its cached response current for the request | 64 | // Should return YES if the cache considers its cached response current for the request |
| @@ -69,6 +69,9 @@ typedef enum _ASICacheStoragePolicy { | @@ -69,6 +69,9 @@ typedef enum _ASICacheStoragePolicy { | ||
| 69 | // When a non-zero maxAge is passed, it should be used as the expiry time for the cached response | 69 | // When a non-zero maxAge is passed, it should be used as the expiry time for the cached response |
| 70 | - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; | 70 | - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; |
| 71 | 71 | ||
| 72 | +// Removes cached data for a particular url | ||
| 73 | +- (void)removeCachedDataForURL:(NSURL *)url; | ||
| 74 | + | ||
| 72 | // Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache | 75 | // Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache |
| 73 | - (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url; | 76 | - (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url; |
| 74 | 77 |
| @@ -17,6 +17,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -17,6 +17,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 17 | 17 | ||
| 18 | @interface ASIDownloadCache () | 18 | @interface ASIDownloadCache () |
| 19 | + (NSString *)keyForURL:(NSURL *)url; | 19 | + (NSString *)keyForURL:(NSURL *)url; |
| 20 | +- (NSString *)pathToFile:(NSString *)file; | ||
| 20 | @end | 21 | @end |
| 21 | 22 | ||
| 22 | @implementation ASIDownloadCache | 23 | @implementation ASIDownloadCache |
| @@ -142,39 +143,21 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -142,39 +143,21 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 142 | 143 | ||
| 143 | - (NSString *)pathToCachedResponseDataForURL:(NSURL *)url | 144 | - (NSString *)pathToCachedResponseDataForURL:(NSURL *)url |
| 144 | { | 145 | { |
| 145 | - [[self accessLock] lock]; | ||
| 146 | - if (![self storagePath]) { | ||
| 147 | - [[self accessLock] unlock]; | ||
| 148 | - return nil; | ||
| 149 | - } | ||
| 150 | // Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view | 146 | // Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view |
| 151 | NSString *extension = [[url path] pathExtension]; | 147 | NSString *extension = [[url path] pathExtension]; |
| 152 | if (![extension length]) { | 148 | if (![extension length]) { |
| 153 | extension = @"html"; | 149 | extension = @"html"; |
| 154 | } | 150 | } |
| 155 | - | 151 | + return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]]; |
| 156 | - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; | ||
| 157 | - | ||
| 158 | - // Look in the session store | ||
| 159 | - NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; | ||
| 160 | - NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]]; | ||
| 161 | - if ([fileManager fileExistsAtPath:dataPath]) { | ||
| 162 | - [[self accessLock] unlock]; | ||
| 163 | - return dataPath; | ||
| 164 | - } | ||
| 165 | - // Look in the permanent store | ||
| 166 | - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; | ||
| 167 | - dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]]; | ||
| 168 | - if ([fileManager fileExistsAtPath:dataPath]) { | ||
| 169 | - [[self accessLock] unlock]; | ||
| 170 | - return dataPath; | ||
| 171 | - } | ||
| 172 | - [[self accessLock] unlock]; | ||
| 173 | - return nil; | ||
| 174 | } | 152 | } |
| 175 | 153 | ||
| 176 | - (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url | 154 | - (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url |
| 177 | { | 155 | { |
| 156 | + return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]]; | ||
| 157 | +} | ||
| 158 | + | ||
| 159 | +- (NSString *)pathToFile:(NSString *)file | ||
| 160 | +{ | ||
| 178 | [[self accessLock] lock]; | 161 | [[self accessLock] lock]; |
| 179 | if (![self storagePath]) { | 162 | if (![self storagePath]) { |
| 180 | [[self accessLock] unlock]; | 163 | [[self accessLock] unlock]; |
| @@ -184,15 +167,13 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -184,15 +167,13 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 184 | NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; | 167 | NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; |
| 185 | 168 | ||
| 186 | // Look in the session store | 169 | // Look in the session store |
| 187 | - NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; | 170 | + NSString *dataPath = [[[self storagePath] stringByAppendingPathComponent:sessionCacheFolder] stringByAppendingPathComponent:file]; |
| 188 | - NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]]; | ||
| 189 | if ([fileManager fileExistsAtPath:dataPath]) { | 171 | if ([fileManager fileExistsAtPath:dataPath]) { |
| 190 | [[self accessLock] unlock]; | 172 | [[self accessLock] unlock]; |
| 191 | return dataPath; | 173 | return dataPath; |
| 192 | } | 174 | } |
| 193 | // Look in the permanent store | 175 | // Look in the permanent store |
| 194 | - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; | 176 | + dataPath = [[[self storagePath] stringByAppendingPathComponent:permanentCacheFolder] stringByAppendingPathComponent:file]; |
| 195 | - dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]]; | ||
| 196 | if ([fileManager fileExistsAtPath:dataPath]) { | 177 | if ([fileManager fileExistsAtPath:dataPath]) { |
| 197 | [[self accessLock] unlock]; | 178 | [[self accessLock] unlock]; |
| 198 | return dataPath; | 179 | return dataPath; |
| @@ -201,6 +182,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -201,6 +182,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 201 | return nil; | 182 | return nil; |
| 202 | } | 183 | } |
| 203 | 184 | ||
| 185 | + | ||
| 204 | - (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request | 186 | - (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request |
| 205 | { | 187 | { |
| 206 | [[self accessLock] lock]; | 188 | [[self accessLock] lock]; |
| @@ -234,32 +216,32 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -234,32 +216,32 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 234 | return path; | 216 | return path; |
| 235 | } | 217 | } |
| 236 | 218 | ||
| 237 | - | 219 | +- (void)removeCachedDataForURL:(NSURL *)url |
| 238 | -- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request | ||
| 239 | { | 220 | { |
| 240 | [[self accessLock] lock]; | 221 | [[self accessLock] lock]; |
| 241 | if (![self storagePath]) { | 222 | if (![self storagePath]) { |
| 242 | [[self accessLock] unlock]; | 223 | [[self accessLock] unlock]; |
| 243 | return; | 224 | return; |
| 244 | } | 225 | } |
| 226 | + NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; | ||
| 245 | 227 | ||
| 246 | - NSString *cachedHeadersPath = [self pathToCachedResponseHeadersForURL:[request url]]; | 228 | + NSString *path = [self pathToCachedResponseHeadersForURL:url]; |
| 247 | - if (!cachedHeadersPath) { | 229 | + if (path) { |
| 248 | - [[self accessLock] unlock]; | 230 | + [fileManager removeItemAtPath:path error:NULL]; |
| 249 | - return; | ||
| 250 | - } | ||
| 251 | - NSString *dataPath = [self pathToCachedResponseDataForURL:[request url]]; | ||
| 252 | - if (!dataPath) { | ||
| 253 | - [[self accessLock] unlock]; | ||
| 254 | - return; | ||
| 255 | } | 231 | } |
| 256 | 232 | ||
| 257 | - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; | 233 | + path = [self pathToCachedResponseDataForURL:url]; |
| 258 | - [fileManager removeItemAtPath:cachedHeadersPath error:NULL]; | 234 | + if (path) { |
| 259 | - [fileManager removeItemAtPath:dataPath error:NULL]; | 235 | + [fileManager removeItemAtPath:path error:NULL]; |
| 236 | + } | ||
| 260 | [[self accessLock] unlock]; | 237 | [[self accessLock] unlock]; |
| 261 | } | 238 | } |
| 262 | 239 | ||
| 240 | +- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request | ||
| 241 | +{ | ||
| 242 | + [self removeCachedDataForURL:[request url]]; | ||
| 243 | +} | ||
| 244 | + | ||
| 263 | - (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request | 245 | - (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request |
| 264 | { | 246 | { |
| 265 | [[self accessLock] lock]; | 247 | [[self accessLock] lock]; |
| @@ -411,6 +393,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | @@ -411,6 +393,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; | ||
| 411 | return YES; | 393 | return YES; |
| 412 | } | 394 | } |
| 413 | 395 | ||
| 396 | + | ||
| 414 | // Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa | 397 | // Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa |
| 415 | + (NSString *)keyForURL:(NSURL *)url | 398 | + (NSString *)keyForURL:(NSURL *)url |
| 416 | { | 399 | { |
| @@ -164,9 +164,32 @@ | @@ -164,9 +164,32 @@ | ||
| 164 | success = ![request didUseCachedResponse]; | 164 | success = ![request didUseCachedResponse]; |
| 165 | GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use"); | 165 | GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use"); |
| 166 | 166 | ||
| 167 | - success = !![request error]; | 167 | + success = ([request error] != nil); |
| 168 | GHAssertTrue(success,@"Request had no error set"); | 168 | GHAssertTrue(success,@"Request had no error set"); |
| 169 | 169 | ||
| 170 | + // Cache some data | ||
| 171 | + NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]; | ||
| 172 | + request = [ASIHTTPRequest requestWithURL:url]; | ||
| 173 | + [request startSynchronous]; | ||
| 174 | + | ||
| 175 | + NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request]; | ||
| 176 | + success = (path != nil); | ||
| 177 | + GHAssertTrue(success,@"Cache failed to store data"); | ||
| 178 | + | ||
| 179 | + path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseHeadersForRequest:request]; | ||
| 180 | + success = (path != nil); | ||
| 181 | + GHAssertTrue(success,@"Cache failed to store data"); | ||
| 182 | + | ||
| 183 | + // Make sure data gets removed | ||
| 184 | + [[ASIDownloadCache sharedCache] removeCachedDataForURL:url]; | ||
| 185 | + | ||
| 186 | + path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]; | ||
| 187 | + success = (path == nil); | ||
| 188 | + GHAssertTrue(success,@"Cache failed to remove data"); | ||
| 189 | + | ||
| 190 | + path = [[ASIDownloadCache sharedCache] pathToCachedResponseHeadersForURL:url]; | ||
| 191 | + success = (path == nil); | ||
| 192 | + GHAssertTrue(success,@"Cache failed to remove data"); | ||
| 170 | 193 | ||
| 171 | // Test ASIDontLoadCachePolicy | 194 | // Test ASIDontLoadCachePolicy |
| 172 | [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; | 195 | [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; |
-
Please register or login to post a comment