Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make cache-deletion asynchronous.

  • Loading branch information...
commit 4c21ae36bb1edf553337625223b2314ef9df44cc 1 parent 1af3cd8
Luke Redpath authored
Showing with 38 additions and 12 deletions.
  1. +38 −12 EGOCache.m
View
50 EGOCache.m
@@ -33,6 +33,14 @@
static id __instance;
+@interface EGOCache ()
+- (void)removeItemFromCache:(NSString*)key;
+- (void)performDiskWriteOperation:(NSInvocation *)invoction;
+- (void)saveCacheDictionary;
+@end
+
+#pragma mark -
+
@implementation EGOCache
+ (EGOCache*)currentCache {
@@ -86,8 +94,15 @@ - (void)clearCache:(NSString*)key {
}
- (void)removeItemFromCache:(NSString*)key {
- [[NSFileManager defaultManager] removeItemAtPath:cachePathForKey(key) error:NULL];
- [cacheDictionary removeObectForKey:key]
+ NSString *cachePath = cachePathForKey(key);
+
+ NSInvocation *deleteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(deleteDataAtPath:)]];
+ [deleteInvocation setTarget:self];
+ [deleteInvocation setSelector:@selector(deleteDataAtPath:)];
+ [deleteInvocation setArgument:&cachePath atIndex:2];
+
+ [self performDiskWriteOperation:deleteInvocation];
+ [cacheDictionary removeObjectForKey:key];
}
- (BOOL)hasCacheForKey:(NSString*)key {
@@ -106,16 +121,13 @@ - (void)setData:(NSData*)data forKey:(NSString*)key {
- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
NSString *cachePath = cachePathForKey(key);
- NSInvocation *diskWriteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];
- [diskWriteInvocation setTarget:self];
- [diskWriteInvocation setSelector:@selector(writeData:toPath:)];
- [diskWriteInvocation setArgument:&data atIndex:2];
- [diskWriteInvocation setArgument:&cachePath atIndex:3];
-
- NSInvocationOperation *diskWriteOperation = [[NSInvocationOperation alloc] initWithInvocation:diskWriteInvocation];
- [diskOperationQueue addOperation:diskWriteOperation];
- [diskWriteOperation release];
-
+ NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];
+ [writeInvocation setTarget:self];
+ [writeInvocation setSelector:@selector(writeData:toPath:)];
+ [writeInvocation setArgument:&data atIndex:2];
+ [writeInvocation setArgument:&cachePath atIndex:3];
+
+ [self performDiskWriteOperation:writeInvocation];
[cacheDictionary setObject:[NSDate dateWithTimeIntervalSinceNow:timeoutInterval] forKey:key];
[self performSelectorOnMainThread:@selector(saveAfterDelay) withObject:nil waitUntilDone:YES]; // Need to make sure the save delay get scheduled in the main runloop, not the current threads
@@ -139,6 +151,11 @@ - (void)writeData:(NSData*)data toPath:(NSString *)path;
[data writeToFile:path atomically:YES];
}
+- (void)deleteDataAtPath:(NSString *)path
+{
+ [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
+}
+
- (void)saveCacheDictionary {
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
@@ -197,6 +214,15 @@ - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NS
#endif
#pragma mark -
+#pragma mark Disk writing operations
+
+- (void)performDiskWriteOperation:(NSInvocation *)invoction {
+ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction];
+ [diskOperationQueue addOperation:operation];
+ [operation release];
+}
+
+#pragma mark -
- (void)dealloc {
[diskOperationQueue release];
Please sign in to comment.
Something went wrong with that request. Please try again.