enormego (owner)

Forks

Revisions

gist: 141427 Download_button fork
public
Description:
Caching for Objective-C/iPhone -- All new updates will be here: http://github.com/enormego/EGOCache
Public Clone URL: git://gist.github.com/141427.git
Embed All Files: show embed
EGOCache.h #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// EGOCache.h
// enormego
//
// Created by Shaun Harrison on 7/4/09.
// Copyright 2009 enormego. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
 
@interface EGOCache : NSObject {
@private
NSMutableDictionary* cacheDictionary;
}
 
+ (EGOCache*)currentCache;
 
- (BOOL)hasCacheForKey:(NSString*)key;
 
- (NSData*)dataForKey:(NSString*)key;
- (void)setData:(NSData*)data forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
 
- (NSString*)stringForKey:(NSString*)key;
- (void)setString:(NSString*)aString forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
 
#if TARGET_OS_IPHONE
- (UIImage*)imageForKey:(NSString*)key;
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#else
- (NSImage*)imageForKey:(NSString*)key;
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#endif
 
@end
 
EGOCache.m #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// EGOCache.m
// enormego
//
// Created by Shaun Harrison on 7/4/09.
// Copyright 2009 enormego. All rights reserved.
//
 
#import "EGOCache.h"
 
#define cachePathForKey(key) [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/EGOCache/%@", key]]
 
static id __instance;
 
@implementation EGOCache
 
+ (EGOCache*)currentCache {
@synchronized(self) {
if(!__instance) {
__instance = [[EGOCache alloc] init];
}
}
 
return __instance;
}
 
- (id)init {
if((self = [super init])) {
NSDictionary* dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"EGOCache"];
if([dict isKindOfClass:[NSDictionary class]]) {
cacheDictionary = [dict mutableCopy];
} else {
cacheDictionary = [[NSMutableDictionary alloc] init];
}
 
[[NSFileManager defaultManager] createDirectoryAtPath:cachePathForKey(@"")
withIntermediateDirectories:YES
attributes:nil
error:NULL];
 
for(NSString* key in cacheDictionary) {
NSDate* date = [cacheDictionary objectForKey:key];
if([[[NSDate date] earlierDate:date] isEqualToDate:date]) {
[[NSFileManager defaultManager] removeItemAtPath:cachePathForKey(key) error:NULL];
}
}
}
 
return self;
}
 
- (BOOL)hasCacheForKey:(NSString*)key {
NSDate* date = [cacheDictionary objectForKey:key];
if(!date) return NO;
if([[[NSDate date] earlierDate:date] isEqualToDate:date]) return NO;
return [[NSFileManager defaultManager] fileExistsAtPath:cachePathForKey(key)];
}
 
#pragma mark -
#pragma mark Data methods
 
- (void)setData:(NSData*)data forKey:(NSString*)key {
[self setData:data forKey:key withTimeoutInterval:60 * 60 * 24];
}
 
- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[data writeToFile:cachePathForKey(key) atomically:YES];
[cacheDictionary setObject:[NSDate dateWithTimeIntervalSinceNow:timeoutInterval] forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
 
- (NSData*)dataForKey:(NSString*)key {
if([self hasCacheForKey:key]) {
return [NSData dataWithContentsOfFile:cachePathForKey(key) options:0 error:NULL];
} else {
return nil;
}
}
 
#pragma mark -
#pragma mark String methods
 
- (NSString*)stringForKey:(NSString*)key {
return [[[NSString alloc] initWithData:[self dataForKey:key] encoding:NSUTF8StringEncoding] autorelease];
}
 
- (void)setString:(NSString*)aString forKey:(NSString*)key {
[self setString:aString forKey:key withTimeoutInterval:60 * 60 * 24];
}
 
- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:timeoutInterval];
}
 
#pragma mark -
#pragma mark Image methds
 
#if TARGET_OS_IPHONE
 
- (UIImage*)imageForKey:(NSString*)key {
return [UIImage imageWithData:[self dataForKey:key]];
}
 
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key {
[self setImage:anImage forKey:key withTimeoutInterval:60 * 60 * 24];
}
 
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[self setData:UIImagePNGRepresentation(anImage) forKey:key withTimeoutInterval:timeoutInterval];
}
 
 
#else
 
- (NSImage*)imageForKey:(NSString*)key {
return [[[NSImage alloc] initWithData:[self dataForKey:key]] autorelease];
}
 
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key {
[self setImage:anImage forKey:key withTimeoutInterval:60 * 60 * 24];
}
 
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil]
forKey:key withTimeoutInterval:timeoutInterval];
}
 
#endif
 
#pragma mark -
 
- (void)dealloc {
[cacheDictionary release];
[super dealloc];
}
 
@end