- JSCache is a simple and fast thread-safe key-value cache class to easily archive objects and cache images to disk.
- It is built on top of EGOCache
- It can be easily turned off for debugging just by setting
#define JSCACHE_ENABLED 0
- The default cache durations are these (but can be easily changed)
#define kJSCacheImageCacheDurationInSeconds 1296000 // 15 days #define kJSCacheDataCacheDurationInSeconds 604800 // 7 days
- You can get a shared instance by calling:
+ (JSCache *)sharedCache;
- To cache objects you simply call -cacheObject:forKey: passing an object which can be serialized (conforms to the NSCoding protocol)
- (void)cacheObject:(id<NSObject, NSCoding>)object forKey:(NSString *)key;
- This method will archive the object in a background queue for avoiding unnecessarily blocking the UI and then pass the NSData to EGOCache to save it to disk.
- You can later on access that object by calling -cachedObjectForKey: and passing the same key.
- To cache an image object you call -cacheImage:forKey: with an UIImage object or -cacheImageData:forKey: with an NSData representing the image. Usually you will want the URL from where you got the image to be the key.
- (void)cacheImage:(UIImage *)image forKey:(NSString *)key; - (void)cacheImageData:(NSData *)data forKey:(NSString *)key;
- And then to retrieve a cached image you just call -cachedImageForKey: or -cachedImageDataForKey:.
Invalidation of cached objects
- You can call -invalidateAllCachedObjects to purge the whole cache, or you can call -invalidateCachedObjectForKey: to invalidate a certain cached object.
- (void)invalidateCachedObjectForKey:(NSString *)key; - (void)invalidateAllCachedObjects;