Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created April 18, 2012 15:53
Show Gist options
  • Save rsaunders100/2414490 to your computer and use it in GitHub Desktop.
Save rsaunders100/2414490 to your computer and use it in GitHub Desktop.
Persistently stores and recovers NSCoding compliant objects with a expiry time
//
// KDPersistantCache.h
// KDPrototype
// Stores NSCoding compliant objects persistantly in the NSCachesDirectory
// for a specified period of time.
//
// Created by on 18/04/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface KDPersistantCache : NSObject
// Cache length is |NSTimeInterval| which is measured in seconds.
- (void) addObject:(id<NSCoding>)objectToCache
forCacheKey:(NSString*)cacheKey
cahceLength:(NSTimeInterval)timeToLive;
// Will reutrn nill if the object donst exist or is expired.
- (id) objectForCacheKey:(NSString*)cacheKey;
// Goes though each object,
// if the date is passed the time to live
// removes the object from the persistant store
- (void) purgeExpiredCahceItems;
@end
//
// KDPersistantCache.m
// KDPrototype
//
// Created by on 18/04/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "KDPersistantCache.h"
@interface KDPersistantCache()
@property (copy, nonatomic) NSString* cachePath;
@end
@implementation KDPersistantCache
@synthesize cachePath = m_cachePath;
- (NSString*) cachePath
{
if (!m_cachePath) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
m_cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:m_cachePath isDirectory:&isDir]
&& isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:m_cachePath
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
}
return m_cachePath;
}
- (NSString*) pathForCahceKey:(NSString*)cacheKey
{
NSString* path = [[self cachePath] stringByAppendingPathComponent:cacheKey];
path = [path stringByAppendingPathExtension:@"kdioscache"];
return path;
}
- (void) addObject:(id<NSCoding>)objectToCache
forCacheKey:(NSString*)cacheKey
cahceLength:(NSTimeInterval)timeToLive
{
NSString* path = [self pathForCahceKey:cacheKey];
NSDate* expireTime = [NSDate dateWithTimeIntervalSinceNow:timeToLive];
NSData* objectData = [NSKeyedArchiver archivedDataWithRootObject:objectToCache];
NSDictionary* objectAndExpireTime = [NSDictionary dictionaryWithObjectsAndKeys:
expireTime, @"expireTime",
objectData, @"object" , nil];
[objectAndExpireTime writeToFile:path atomically:YES];
}
- (id) objectForCacheKey:(NSString*)cacheKey
{
NSString* path = [self pathForCahceKey:cacheKey];
NSDictionary* objectAndExpireTime = [NSDictionary dictionaryWithContentsOfFile:path];
if (objectAndExpireTime)
{
NSDate* expireTime = [objectAndExpireTime objectForKey:@"expireTime"];
// Ensure the cache has not expired
if ([expireTime laterDate:[NSDate date]] == expireTime)
{
NSData* objectData = [objectAndExpireTime objectForKey:@"object"];
id objectRecoveredFromCache = [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
return objectRecoveredFromCache;
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment