This repository has been archived by the owner on Apr 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathECDataCache.m
211 lines (159 loc) · 4.78 KB
/
ECDataCache.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
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
//
// ECDataCache.m
// ECDataCache
//
// Created by Chris Streeter on 2/27/13.
// Copyright 2013 Educreations, Inc. All rights reserved.
//
#import "ECDataCache.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString (ECDataCacheMD5)
- (NSString *)ec_MD5
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(data.bytes, (unsigned int)data.length, md5Buffer);
// Convert unsigned char buffer to NSString of hex values
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", md5Buffer[i]];
}
return output;
}
@end
@implementation ECDataCache
+ (instancetype)sharedCache
{
static ECDataCache *sharedCache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedCache = [[ECDataCache alloc] init];
});
return sharedCache;
}
#pragma mark - Helpers
+ (NSString *)cacheDirectory
{
static NSString *cacheDirectory = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *directories = [fileManager URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask];
NSURL *cacheURL = [directories lastObject];
NSURL *url = [cacheURL URLByAppendingPathComponent:@"ECDataCache"
isDirectory:YES];
cacheDirectory = [[url path] copy];
if (![fileManager fileExistsAtPath:cacheDirectory]) {
[fileManager createDirectoryAtPath:cacheDirectory
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
});
return cacheDirectory;
}
+ (NSString *)keyForURL:(NSURL *)url
{
return [url absoluteString];
}
+ (NSString *)pathForKey:(NSString *)key
{
NSString *fileName = [key ec_MD5];
return [[self cacheDirectory] stringByAppendingPathComponent:fileName];
}
#pragma mark - Getters
- (NSData *)dataForKey:(NSString *)key
{
if (!key) {
return nil;
}
NSData *data = [super objectForKey:key];
if (data) {
return data;
}
return [self dataFromDiskForKey:key];
}
- (NSData *)dataForURL:(NSURL *)url
{
return [self dataForKey:[[self class] keyForURL:url]];
}
#pragma mark - Setters
- (void)setData:(NSData *)data forKey:(NSString *)key
{
if (!key || !data) {
return;
}
// Set the NSCache store
[super setObject:data forKey:key];
// Set the disk cache
[self setDataOnDisk:data forKey:key];
}
- (void)setData:(NSData *)data forURL:(NSURL *)url
{
[self setData:data forKey:[[self class] keyForURL:url]];
}
#pragma mark - Deleters
- (void)removeDataForKey:(NSString *)key
{
if (!key) {
return;
}
// Remove the NSCache object
[super removeObjectForKey:key];
// Remove the disk cache
[self removeDataOnDiskForKey:key];
}
- (void)removeDataForURL:(NSURL *)url
{
[self removeDataForKey:[[self class] keyForURL:url]];
}
#pragma mark - Disk Interface
static dispatch_queue_t get_disk_cache_queue()
{
static dispatch_queue_t diskCacheQueue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
diskCacheQueue = dispatch_queue_create("com.educreations.disk-cache.processing", NULL);
});
return diskCacheQueue;
}
static dispatch_queue_t get_disk_io_queue()
{
static dispatch_queue_t diskIOQueue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
diskIOQueue = dispatch_queue_create("com.educreations.disk-cache.io", NULL);
});
return diskIOQueue;
}
- (NSData *)dataFromDiskForKey:(NSString *)key
{
NSString *path = [[self class] pathForKey:key];
__block NSData *response = nil;
dispatch_sync(get_disk_cache_queue(), ^{
response = [NSData dataWithContentsOfFile:path
options:0
error:nil];
});
return response;
}
- (void)setDataOnDisk:(NSData *)data forKey:(NSString *)key
{
NSString *path = [[self class] pathForKey:key];
dispatch_async(get_disk_io_queue(), ^{
[data writeToFile:path
atomically:YES];
});
}
- (void)removeDataOnDiskForKey:(NSString *)key
{
NSString *path = [[self class] pathForKey:key];
dispatch_async(get_disk_io_queue(), ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:path error:nil];
});
}
@end