diff --git a/MKNetworkKit/MKNetworkEngine.h b/MKNetworkKit/MKNetworkEngine.h index 1b42664..3eb94ab 100644 --- a/MKNetworkKit/MKNetworkEngine.h +++ b/MKNetworkKit/MKNetworkEngine.h @@ -384,6 +384,18 @@ */ -(int) cacheMemoryCost; + +/*! + * @abstract Cache Directory In Disk Size + * + * @discussion + * This method can be over-ridden by subclasses to provide an alternative in disk cache size. + * By default, MKNetworkKit caches MKNETWORKCACHE_DEFAULT_DISK_SIZE bytes in disk cache + * The default size is MKNETWORKCACHE_DEFAULT_DISK_SIZE + * Overriding this method is optional + */ +-(unsigned long long int) cacheDiskSize; + /*! * @abstract Enable Caching * diff --git a/MKNetworkKit/MKNetworkEngine.m b/MKNetworkKit/MKNetworkEngine.m index a238f97..ba3dcc3 100644 --- a/MKNetworkKit/MKNetworkEngine.m +++ b/MKNetworkKit/MKNetworkEngine.m @@ -618,6 +618,11 @@ -(int) cacheMemoryCost { return MKNETWORKCACHE_DEFAULT_COST; } +-(unsigned long long int) cacheDiskSize{ + + return MKNETWORKCACHE_DEFAULT_DISK_SIZE; +} + -(void) saveCache { for(NSString *cacheKey in [self.memoryCache allKeys]) @@ -723,6 +728,12 @@ -(void) useCache { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCache) name:UIApplicationWillTerminateNotification object:nil]; + + //check for emptyCache whenever coming to app + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(emptyCache) + name:UIApplicationDidBecomeActiveNotification + object:nil]; #elif TARGET_OS_MAC @@ -749,12 +760,37 @@ -(void) emptyCache { contentsOfDirectoryAtPath:[self cacheDirectoryName] error:&error]; if(error) DLog(@"%@", error); - error = nil; + error = nil; + + unsigned long long int folderSize = [self folderSize:[self cacheDirectoryName]]; + unsigned long long int sizeToRemove = folderSize - [self cacheDiskSize]; + + if (folderSize <= 0) { + //return if we have not exceeded the limit + return; + } + + unsigned long long int fileSize = 0; + for(NSString *fileName in directoryContents) { NSString *path = [[self cacheDirectoryName] stringByAppendingPathComponent:fileName]; + + //calcualte size and add to count + NSDictionary *fileDictionary = [[NSFileManager defaultManager] + attributesOfItemAtPath:path + error:&error]; + + fileSize += [fileDictionary fileSize]; + + //remove file [[NSFileManager defaultManager] removeItemAtPath:path error:&error]; if(error) DLog(@"%@", error); + + //break if we are back in limit + if (fileSize > sizeToRemove) { + break; + } } error = nil; @@ -763,4 +799,25 @@ -(void) emptyCache { if(error) DLog(@"%@", error); } +- (unsigned long long int)folderSize:(NSString *)folderPath { + NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil]; + NSEnumerator *filesEnumerator = [filesArray objectEnumerator]; + NSString *fileName; + unsigned long long int fileSize = 0; + + while (fileName = [filesEnumerator nextObject]) { + + NSString *path = [[self cacheDirectoryName] stringByAppendingPathComponent:fileName]; + NSError *error = nil; + + NSDictionary *fileDictionary = [[NSFileManager defaultManager] + attributesOfItemAtPath:path + error:&error]; + + fileSize += [fileDictionary fileSize]; + } + + return fileSize; +} + @end diff --git a/MKNetworkKit/MKNetworkKit.h b/MKNetworkKit/MKNetworkKit.h index 523e6ab..250e6c8 100644 --- a/MKNetworkKit/MKNetworkKit.h +++ b/MKNetworkKit/MKNetworkKit.h @@ -82,6 +82,7 @@ #define kMKNetworkEngineOperationCountChanged @"kMKNetworkEngineOperationCountChanged" #define MKNETWORKCACHE_DEFAULT_COST 10 +#define MKNETWORKCACHE_DEFAULT_DISK_SIZE 100*1024*1024 //100 MB #define MKNETWORKCACHE_DEFAULT_DIRECTORY @"MKNetworkKitCache" #define kMKNetworkKitDefaultCacheDuration 60 // 1 minute #define kMKNetworkKitDefaultImageHeadRequestDuration 3600*24*1 // 1 day (HEAD requests with eTag are sent only after expiry of this. Not that these are not RFC compliant, but needed for performance tuning)