-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIInMemoryCache.m
500 lines (366 loc) · 15.6 KB
/
NIInMemoryCache.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NIInMemoryCache.h"
#import "NIDataStructures.h"
#import "NIDebuggingTools.h"
#import "NIPreprocessorMacros.h"
@interface NIMemoryCache()
@property (nonatomic, readwrite, retain) NSMutableDictionary* cacheMap;
@property (nonatomic, readwrite, retain) NILinkedList* lruCacheObjects;
@end
/**
* @brief A single cache item's information.
*
* Used in expiration calculations and for storing the actual cache object.
*/
@interface NIMemoryCacheInfo : NSObject {
@private
NSString* _name;
id _object;
NSDate* _expirationDate;
NSDate* _lastAccessTime;
// Keep tabs on the location of the lru object so that we can move it quickly.
NILinkedListLocation* _lruLocation;
}
/**
* @brief The name used to store this object in the cache.
*/
@property (nonatomic, readwrite, copy) NSString* name;
/**
* @brief The object stored in the cache.
*/
@property (nonatomic, readwrite, retain) id object;
/**
* @brief The date after which the image is no longer valid and should be removed from the cache.
*/
@property (nonatomic, readwrite, retain) NSDate* expirationDate;
/**
* @brief The last time this image was accessed.
*
* This property is updated every time the image is fetched from or stored into the cache. It
* is used when the memory peak has been reached as a fast means of removing least-recently-used
* images. When the memory limit is reached, we sort the cache based on the last access times and
* then prune images until we're under the memory limit again.
*/
@property (nonatomic, readwrite, retain) NSDate* lastAccessTime;
/**
* @brief The location of this object in the least-recently used linked list.
*/
@property (nonatomic, readwrite, assign) NILinkedListLocation* lruLocation;
/**
* @brief Determine whether this cache entry has past its expiration date.
*
* @returns YES if an expiration date has been specified and the expiration date has been passed.
* NO in all other cases. Notably if there is no expiration date then this object will
* never expire.
*/
- (BOOL)hasExpired;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NIMemoryCache
@synthesize cacheMap = _cacheMap;
@synthesize lruCacheObjects = _lruCacheObjects;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
NI_RELEASE_SAFELY(_cacheMap);
NI_RELEASE_SAFELY(_lruCacheObjects);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init {
return [self initWithCapacity:0];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithCapacity:(NSUInteger)capacity {
if ((self = [super init])) {
_cacheMap = [[NSMutableDictionary alloc] initWithCapacity:capacity];
_lruCacheObjects = [[NILinkedList alloc] init];
// Automatically reduce memory usage when we get a memory warning.
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(reduceMemoryUsage)
name: UIApplicationDidReceiveMemoryWarningNotification
object: nil];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Internal Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateAccessTimeForInfo:(NIMemoryCacheInfo *)info {
NIDASSERT(nil != info);
if (nil == info) {
return;
}
info.lastAccessTime = [NSDate date];
[_lruCacheObjects removeObjectAtLocation:info.lruLocation];
info.lruLocation = [_lruCacheObjects addObject:info];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NIMemoryCacheInfo *)cacheInfoForName:(NSString *)name {
return [_cacheMap objectForKey:name];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setCacheInfo:(NIMemoryCacheInfo *)info forName:(NSString *)name {
NIDASSERT(nil != name);
if (nil == name) {
return;
}
// Storing in the cache counts as an access of the object, so we update the access time.
[self updateAccessTimeForInfo:info];
[self willSetObject: info.object
withName: name
previousObject: [self cacheInfoForName:name].object];
[_cacheMap setObject:info forKey:name];
[self didSetObject: info.object
withName: name];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeCacheInfoForName:(NSString *)name {
NIDASSERT(nil != name);
if (nil == name) {
return;
}
[self willRemoveObject:[self cacheInfoForName:name].object withName:name];
[_cacheMap removeObjectForKey:name];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Subclassing
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willSetObject:(id)object withName:(NSString *)name previousObject:(id)previousObject {
// No-op
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didSetObject:(id)object withName:(NSString *)name {
// No-op
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willRemoveObject:(id)object withName:(NSString *)name {
// No-op
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)storeObject:(id)object withName:(NSString *)name {
[self storeObject:object withName:name expiresAfter:nil];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)storeObject:(id)object withName:(NSString *)name expiresAfter:(NSDate *)expirationDate {
// Don't store nil objects in the cache.
if (nil == object) {
return;
}
if (nil != expirationDate && [[NSDate date] timeIntervalSinceDate:expirationDate] >= 0) {
// The object being stored is already expired so remove the object from the cache altogether.
[self removeObjectWithName:name];
// We're done here.
return;
}
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
// Create a new cache entry.
if (nil == info) {
info = [[[NIMemoryCacheInfo alloc] init] autorelease];
info.name = name;
}
// Store the object in the cache item.
info.object = object;
// Override any existing expiration date.
info.expirationDate = expirationDate;
// Commit the changes to the cache.
[self setCacheInfo:info forName:name];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)objectWithName:(NSString *)name {
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
id object = nil;
if (nil != info) {
if ([info hasExpired]) {
[self removeObjectWithName:name];
} else {
// Update the access time whenever we fetch an object from the cache.
[self updateAccessTimeForInfo:info];
object = info.object;
}
}
return [[object retain] autorelease];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)containsObjectWithName:(NSString *)name {
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
if ([info hasExpired]) {
[self removeObjectWithName:name];
return NO;
}
return (nil != info);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSDate *)dateOfLastAccessWithName:(NSString *)name {
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
if ([info hasExpired]) {
[self removeObjectWithName:name];
return nil;
}
return [info lastAccessTime];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)nameOfLeastRecentlyUsedObject {
NIMemoryCacheInfo* info = [self.lruCacheObjects firstObject];
if ([info hasExpired]) {
[self removeObjectWithName:info.name];
return nil;
}
return info.name;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)nameOfMostRecentlyUsedObject {
NIMemoryCacheInfo* info = [self.lruCacheObjects lastObject];
if ([info hasExpired]) {
[self removeObjectWithName:info.name];
return nil;
}
return info.name;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeObjectWithName:(NSString *)name {
[self removeCacheInfoForName:name];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeAllObjects {
NI_RELEASE_SAFELY(_cacheMap);
_cacheMap = [[NSMutableDictionary alloc] init];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)reduceMemoryUsage {
// Copy the cache map because it's likely that we're going to modify it.
NSDictionary* cacheMap = [_cacheMap copy];
// Iterate over the copied cache map (which will not be modified).
for (id name in cacheMap) {
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
if ([info hasExpired]) {
[self removeCacheInfoForName:name];
}
}
NI_RELEASE_SAFELY(cacheMap);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSUInteger)count {
return [_cacheMap count];
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NIMemoryCacheInfo
@synthesize name = _name;
@synthesize object = _object;
@synthesize expirationDate = _expirationDate;
@synthesize lastAccessTime = _lastAccessTime;
@synthesize lruLocation = _lruLocation;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
NI_RELEASE_SAFELY(_name);
NI_RELEASE_SAFELY(_object);
NI_RELEASE_SAFELY(_expirationDate);
NI_RELEASE_SAFELY(_lastAccessTime);
_lruLocation = nil;
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)hasExpired {
return (nil != _expirationDate
&& [[NSDate date] timeIntervalSinceDate:_expirationDate] >= 0);
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface NIImageMemoryCache()
// Internally only.
@property (nonatomic, readwrite, assign) NSUInteger numberOfPixels;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NIImageMemoryCache
@synthesize numberOfPixels = _numberOfPixels;
@synthesize maxNumberOfPixels = _maxNumberOfPixels;
@synthesize maxNumberOfPixelsUnderStress = _maxNumberOfPixelsUnderStress;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSUInteger)numberOfPixelsUsedByImage:(UIImage *)image {
if (nil == image) {
return 0;
}
NSUInteger numberOfPixels = (NSUInteger)(image.size.width * image.size.height);
if ([image respondsToSelector:@selector(scale)]) {
numberOfPixels *= [image scale];
}
return numberOfPixels;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)reduceMemoryUsage {
// Remove all expired images first.
[super reduceMemoryUsage];
if (self.maxNumberOfPixelsUnderStress > 0) {
// Remove the least recently used images by iterating over the linked list.
while (self.numberOfPixels > self.maxNumberOfPixelsUnderStress) {
NIMemoryCacheInfo* info = [self.lruCacheObjects firstObject];
[self removeCacheInfoForName:info.name];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willSetObject:(id)object withName:(NSString *)name previousObject:(id)previousObject {
NIDASSERT(nil == object || [object isKindOfClass:[UIImage class]]);
if (![object isKindOfClass:[UIImage class]]) {
return;
}
self.numberOfPixels -= [self numberOfPixelsUsedByImage:previousObject];
self.numberOfPixels += [self numberOfPixelsUsedByImage:object];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didSetObject:(id)object withName:(NSString *)name {
// Reduce the cache size after the object has been set in case the cache size is smaller
// than the object that's being added and we need to remove this object right away. If we
// try to reduce the cache size before the object's been set, we won't have anything to remove
// and we'll get stuck in an infinite loop.
if (self.maxNumberOfPixels > 0) {
// Remove least recently used images until we satisfy our memory constraints.
while (self.numberOfPixels > self.maxNumberOfPixels
&& [self.lruCacheObjects count] > 0) {
NIMemoryCacheInfo* info = [self.lruCacheObjects firstObject];
[self removeCacheInfoForName:info.name];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willRemoveObject:(id)object withName:(NSString *)name {
NIDASSERT(nil == object || [object isKindOfClass:[UIImage class]]);
if (nil == object || ![object isKindOfClass:[UIImage class]]) {
return;
}
NIMemoryCacheInfo* info = [self cacheInfoForName:name];
[self.lruCacheObjects removeObjectAtLocation:info.lruLocation];
self.numberOfPixels -= [self numberOfPixelsUsedByImage:object];
}
@end