Skip to content

Commit

Permalink
Merge pull request steipete#13 from tibr/master
Browse files Browse the repository at this point in the history
Adding error handling when trying to unarchive an invalid archive
  • Loading branch information
steipete committed Apr 11, 2012
2 parents 476065e + 23b51d2 commit 42ad9c7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions SDURLCache.m
Expand Up @@ -700,12 +700,23 @@ - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
dispatch_sync(get_disk_cache_queue(), ^{
NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kAFURLCacheInfoAccessesKey];
if ([accesses objectForKey:cacheKey]) { // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS
response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]];
if (response) {
// OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
// on disk now in order to save IO and time
[accesses setObject:[NSDate date] forKey:cacheKey];
_diskCacheInfoDirty = YES;
@try {
response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]];
if (response) {
// OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
// on disk now in order to save IO and time
[accesses setObject:[NSDate date] forKey:cacheKey];
_diskCacheInfoDirty = YES;
}
}
@catch (NSException *exception) {
if ([exception.name isEqualToString:NSInvalidArgumentException]) {
NSLog(@"Could not unarchive object at %@, Invalid archive!", [_diskCachePath stringByAppendingPathComponent:cacheKey]);
[self removeCachedResponseForRequest:request];
}
}
@finally {
// do nothing
}
}
});
Expand Down

0 comments on commit 42ad9c7

Please sign in to comment.