Created
September 23, 2011 22:12
-
-
Save nrj/1238573 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NECacheObject.m | |
// | |
// Created by nrj on 4/28/11. | |
// | |
#import "NECacheObject.h" | |
@implementation NECacheObject | |
@synthesize expireDate, object; | |
+ (NECacheObject *)cacheObject:(id)object expiresMinutesFromNow:(NSInteger)expireMinutes { | |
NECacheObject *instance = [[NECacheObject alloc] init]; | |
[instance setExpireDate:[NSDate dateWithTimeIntervalSinceNow:(expireMinutes * 60)]]; | |
[instance setObject:object]; | |
return [instance autorelease]; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
if ((self = [super init])) { | |
[self setExpireDate:[aDecoder decodeObjectForKey:@"expireDate"]]; | |
[self setObject:[aDecoder decodeObjectForKey:@"object"]]; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"<NECacheObject value:%@ expires:%@>", object, expireDate]; | |
} | |
- (BOOL)isExpired { | |
NSDate *date = [NSDate date]; | |
return ([date compare:expireDate] == NSOrderedDescending); | |
} | |
- (void)expireNow { | |
[self setExpireDate:[NSDate date]]; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder { | |
[aCoder encodeObject:expireDate forKey:@"expireDate"]; | |
[aCoder encodeObject:object forKey:@"object"]; | |
} | |
- (void)dealloc { | |
[expireDate release]; | |
[object release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment