-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUUDataCache.m
More file actions
231 lines (194 loc) · 6 KB
/
UUDataCache.m
File metadata and controls
231 lines (194 loc) · 6 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// UUDataCache.m
// Useful Utilities - UUDataCache for commonly fetched data from URL's
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
#import "UUDataCache.h"
//If you want to provide your own logging mechanism, define UUDebugLog in your .pch
#ifndef UUDebugLog
#ifdef DEBUG
#define UUDebugLog(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#else
#define UUDebugLog(fmt, ...)
#endif
#endif
@implementation UUDataCache
NSTimeInterval uuDataCacheExpirationLength = (60 * 60 * 24 * 30); //30 days
+ (void) uuSetCacheExpirationLength:(NSTimeInterval)seconds
{
uuDataCacheExpirationLength = seconds;
}
+ (NSString*) uuCacheLocation
{
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cachePath = [cachePath stringByAppendingPathComponent:@"UUDataCache"];
NSFileManager* fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:cachePath])
[fm createDirectoryAtPath:cachePath withIntermediateDirectories:TRUE attributes:nil error:nil];
return cachePath;
}
+ (void) uuClearCacheContents
{
NSString* cacheLocation = [UUDataCache uuCacheLocation];
NSFileManager* fm = [NSFileManager defaultManager];
[fm removeItemAtPath:cacheLocation error:nil];
[fm createDirectoryAtPath:cacheLocation withIntermediateDirectories:YES attributes:nil error:nil];
}
+ (bool) uuIsCacheExpiredForUrl:(NSString*)path
{
if (path)
{
NSDate* cachedDate = [[NSUserDefaults standardUserDefaults] objectForKey:path];
if (cachedDate)
{
NSTimeInterval elapsed = -[cachedDate timeIntervalSinceNow];
return (elapsed > uuDataCacheExpirationLength);
}
}
return false;
}
+ (NSData*) uuBundleDataForUrl:(NSURL*)url
{
if (url != nil)
{
NSString* fileName = [url absoluteString];
NSArray* parts = [fileName componentsSeparatedByString:@"/"];
if (parts.count)
fileName = [parts objectAtIndex:[parts count] - 1];
fileName = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
if (fileName)
{
return [NSData dataWithContentsOfFile:fileName];
}
}
return nil;
}
+ (void) uuClearCacheForURL:(NSURL*)url
{
NSString* path = [UUDataCache uuCachePathForURL:url];
NSFileManager* fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:path])
{
NSError* err = nil;
if (![fm removeItemAtPath:path error:&err])
{
UUDebugLog(@"Failed to delete data at cache path: %@", path);
}
}
//Zero out the last date...
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:path];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void) uuPurgeContentAboveSize:(unsigned long long)purgeFileSize
{
NSString* cacheLocation = [self uuCacheLocation];
NSFileManager* fm = [NSFileManager defaultManager];
NSError* err = nil;
NSArray* contents = [fm contentsOfDirectoryAtPath:cacheLocation error:&err];
if (!err && contents)
{
for (NSString* item in contents)
{
NSString* path = [cacheLocation stringByAppendingPathComponent:item];
err = nil;
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:&err];
if (!err && attrs)
{
NSNumber* fileSize = [attrs valueForKey:NSFileSize];
if (fileSize)
{
if (fileSize.unsignedLongLongValue > purgeFileSize)
{
[self uuClearCacheForURL:[NSURL URLWithString:item]];
}
}
}
}
}
}
+ (void) uuPurgeExpiredContent
{
NSString* cacheLocation = [self uuCacheLocation];
NSFileManager* fm = [NSFileManager defaultManager];
NSError* err = nil;
NSArray* contents = [fm contentsOfDirectoryAtPath:cacheLocation error:&err];
if (!err && contents)
{
for (NSString* item in contents)
{
NSURL* url = [NSURL URLWithString:item];
if ([self uuIsCacheExpiredForUrl:[self uuCachePathForURL:url]])
{
[self uuClearCacheForURL:url];
}
}
}
}
+ (NSData*) uuDataForURL:(NSURL*)url
{
NSData* data = nil;
if (url != nil)
{
//First check to see if the data is in the bundle...
data = [UUDataCache uuBundleDataForUrl:url];
//If not, let's check our cache...
if (!data)
{
NSString* path = [UUDataCache uuCachePathForURL:url];
//Make sure the cache isn't expired...
if (![UUDataCache uuIsCacheExpiredForUrl:path])
{
data = [NSData dataWithContentsOfFile:path];
}
else
{
[UUDataCache uuClearCacheForURL:url];
}
}
}
return data;
}
+ (void) uuCacheData:(NSData*)data forURL:(NSURL*)url
{
//Write out the data
NSString* path = [UUDataCache uuCachePathForURL:url];
[data writeToFile:path atomically:YES];
//Store the date
NSDate* now = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:now forKey:path];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSString*) uuCachePathForURL:(NSURL*)url
{
NSString* cacheLocation = [UUDataCache uuCacheLocation];
NSString* path = [url absoluteString];
path = [[path componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@"-"];
path = [cacheLocation stringByAppendingPathComponent:path];
return path;
}
+ (BOOL) uuDoesCachedFileExistForURL:(NSURL*)url
{
NSString* cacheLocation = [UUDataCache uuCachePathForURL:url];
return [[NSFileManager defaultManager] fileExistsAtPath:cacheLocation];
}
+ (UUDataCache*) sharedCache
{
static UUDataCache* theSharedCache = nil;
if (!theSharedCache)
{
theSharedCache = [[UUDataCache alloc] init];
}
return theSharedCache;
}
- (id) objectForKey:(id)key
{
return [UUDataCache uuDataForURL:[NSURL URLWithString:key]];
}
- (void) setObject:(id)object forKey:(id)key
{
[UUDataCache uuCacheData:object forURL:[NSURL URLWithString:key]];
}
@end