Ben Copsey

ASIDownloadCache: Added removeCachedDataForURL: (closes gh-180)

Internal tweaks to remove a bit of duplication
... ... @@ -58,7 +58,7 @@ typedef enum _ASICacheStoragePolicy {
- (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request;
// Should Remove cached data for a particular request
// Removes cached data for a particular request
- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request;
// Should return YES if the cache considers its cached response current for the request
... ... @@ -69,6 +69,9 @@ typedef enum _ASICacheStoragePolicy {
// When a non-zero maxAge is passed, it should be used as the expiry time for the cached response
- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
// Removes cached data for a particular url
- (void)removeCachedDataForURL:(NSURL *)url;
// Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache
- (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url;
... ...
... ... @@ -17,6 +17,7 @@ static NSString *permanentCacheFolder = @"PermanentStore";
@interface ASIDownloadCache ()
+ (NSString *)keyForURL:(NSURL *)url;
- (NSString *)pathToFile:(NSString *)file;
@end
@implementation ASIDownloadCache
... ... @@ -142,39 +143,21 @@ static NSString *permanentCacheFolder = @"PermanentStore";
- (NSString *)pathToCachedResponseDataForURL:(NSURL *)url
{
[[self accessLock] lock];
if (![self storagePath]) {
[[self accessLock] unlock];
return nil;
}
// 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
NSString *extension = [[url path] pathExtension];
if (![extension length]) {
extension = @"html";
}
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
// Look in the session store
NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]];
if ([fileManager fileExistsAtPath:dataPath]) {
[[self accessLock] unlock];
return dataPath;
}
// Look in the permanent store
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]];
if ([fileManager fileExistsAtPath:dataPath]) {
[[self accessLock] unlock];
return dataPath;
}
[[self accessLock] unlock];
return nil;
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]];
}
- (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url
{
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]];
}
- (NSString *)pathToFile:(NSString *)file
{
[[self accessLock] lock];
if (![self storagePath]) {
[[self accessLock] unlock];
... ... @@ -184,15 +167,13 @@ static NSString *permanentCacheFolder = @"PermanentStore";
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
// Look in the session store
NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]];
NSString *dataPath = [[[self storagePath] stringByAppendingPathComponent:sessionCacheFolder] stringByAppendingPathComponent:file];
if ([fileManager fileExistsAtPath:dataPath]) {
[[self accessLock] unlock];
return dataPath;
}
// Look in the permanent store
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
dataPath = [path stringByAppendingPathComponent:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]];
dataPath = [[[self storagePath] stringByAppendingPathComponent:permanentCacheFolder] stringByAppendingPathComponent:file];
if ([fileManager fileExistsAtPath:dataPath]) {
[[self accessLock] unlock];
return dataPath;
... ... @@ -201,6 +182,7 @@ static NSString *permanentCacheFolder = @"PermanentStore";
return nil;
}
- (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request
{
[[self accessLock] lock];
... ... @@ -234,32 +216,32 @@ static NSString *permanentCacheFolder = @"PermanentStore";
return path;
}
- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request
- (void)removeCachedDataForURL:(NSURL *)url
{
[[self accessLock] lock];
if (![self storagePath]) {
[[self accessLock] unlock];
return;
}
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
NSString *cachedHeadersPath = [self pathToCachedResponseHeadersForURL:[request url]];
if (!cachedHeadersPath) {
[[self accessLock] unlock];
return;
}
NSString *dataPath = [self pathToCachedResponseDataForURL:[request url]];
if (!dataPath) {
[[self accessLock] unlock];
return;
NSString *path = [self pathToCachedResponseHeadersForURL:url];
if (path) {
[fileManager removeItemAtPath:path error:NULL];
}
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
[fileManager removeItemAtPath:cachedHeadersPath error:NULL];
[fileManager removeItemAtPath:dataPath error:NULL];
path = [self pathToCachedResponseDataForURL:url];
if (path) {
[fileManager removeItemAtPath:path error:NULL];
}
[[self accessLock] unlock];
}
- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request
{
[self removeCachedDataForURL:[request url]];
}
- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request
{
[[self accessLock] lock];
... ... @@ -411,6 +393,7 @@ static NSString *permanentCacheFolder = @"PermanentStore";
return YES;
}
// Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa
+ (NSString *)keyForURL:(NSURL *)url
{
... ...
... ... @@ -164,9 +164,32 @@
success = ![request didUseCachedResponse];
GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use");
success = !![request error];
success = ([request error] != nil);
GHAssertTrue(success,@"Request had no error set");
// Cache some data
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"];
request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
success = (path != nil);
GHAssertTrue(success,@"Cache failed to store data");
path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseHeadersForRequest:request];
success = (path != nil);
GHAssertTrue(success,@"Cache failed to store data");
// Make sure data gets removed
[[ASIDownloadCache sharedCache] removeCachedDataForURL:url];
path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url];
success = (path == nil);
GHAssertTrue(success,@"Cache failed to remove data");
path = [[ASIDownloadCache sharedCache] pathToCachedResponseHeadersForURL:url];
success = (path == nil);
GHAssertTrue(success,@"Cache failed to remove data");
// Test ASIDontLoadCachePolicy
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
... ...