MKNetworkKit icon indicating copy to clipboard operation
MKNetworkKit copied to clipboard

LRU Caching on Disk

Open muneebali opened this issue 11 years ago • 6 comments

I have implemented LRU caching on disk. Now in emptyCache method, it doesn't remove all the cache and instead removes the cache unless we are in limit set in cacheDiskSize. Also an observer to UIApplicationDidBecomeActiveNotification is added in useCache method so that it removes excess cache whenever app becomes active.

Please review if it works for you. Thanks!

muneebali avatar May 27 '13 09:05 muneebali

hey, is this making it into master? I'm interested...

micahnyc avatar May 29 '13 20:05 micahnyc

Fingers crossed.

muneebali avatar May 30 '13 06:05 muneebali

I'm interested in this. Haven't gotten time to go through and merge the code. Give me some time.

On Thursday, May 30, 2013, muneebali wrote:

Fingers crossed.

— Reply to this email directly or view it on GitHubhttps://github.com/MugunthKumar/MKNetworkKit/pull/343#issuecomment-18663703 .

Regards,

M.Mugunth Kumar W: http://mugunthkumar.com B: http://blog.mugunthkumar.com Singapore

MugunthKumar avatar Jun 02 '13 07:06 MugunthKumar

any news on this?

vittoriom avatar Sep 25 '13 12:09 vittoriom

Not yet

muneebali avatar Sep 25 '13 17:09 muneebali

Ok, I investigated and there are no assumptions to be made on the cache directory, so I'm not sure your emptyCache implementation is a LRU one. To do that, you should do something like

NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL fileURLWithPath:[self cacheDirectoryName]]
                                                              includingPropertiesForKeys:@[NSURLContentModificationDateKey]
                                                                                 options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                                   error:&error];

  NSArray *sortedContent = [directoryContents sortedArrayUsingComparator:
                              ^(NSURL *file1, NSURL *file2)
                              {
                                  // compare
                                  NSDate *file1Date;
                                  [file1 getResourceValue:&file1Date forKey:NSURLContentModificationDateKey error:nil];

                                  NSDate *file2Date;
                                  [file2 getResourceValue:&file2Date forKey:NSURLContentModificationDateKey error:nil];

                                  // Ascending:
                                  return [file1Date compare: file2Date];
                                  // Descending:
                                  //return [file2Date compare: file1Date];
                              }];

And of course iterate on sortedContent instead of directoryContents

:)

vittoriom avatar Sep 26 '13 12:09 vittoriom