TumblrArchive / TMCache

Fast parallel object cache for iOS and OS X.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need my custom policy to clean disk cache..

fvisticot opened this issue · comments

Sometime, I need to clean the disk cache regarding specific rules (not time or size based)

By example, I would like to encode the cache key with attributes, iterate through the cache elements and remove some elements based on the key (encoded) attributes..

Do you think it is doable ?
Any other alternative to iterated the cache elements ?

I would be nice to call a generic "cleanCache" method interface where each developer could implement his own policy..

I've been thinking about a few ways to do this, ultimately I'm going to add something to let you enumerate all the keys. The cleanest way would just be exposing the keys as a readonly property, but then it's up to the developer to make sure they only read the property on the cache's queue (with a barrier, in the case of TMMemoryCache). Another way would be a method like enumerateKeysWithBlock:, which is safer but more limiting. I'll think about it some more.

This has been added in 1.2.0!

Here's an idea for how you'd implement your own clearing mechanism:

self.cache.memoryCache.removeAllObjectsOnMemoryWarning = NO;

self.cache.memoryCache.didReceiveMemoryWarningBlock = ^(TMMemoryCache *cache) {
    [cache enumerateObjectsWithBlock:^(TMMemoryCache *cache, NSString *key, id object) {
        // do your removals asynchronously here
    } completionBlock:nil];
};