forked from Cocoanetics/DTCoreText
/
DTCache.m
81 lines (61 loc) · 1.36 KB
/
DTCache.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
//
// DTCache.m
// CoreTextExtensions
//
// Created by Oliver Drobnik on 7/30/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#import "DTCache.h"
// if we support 3.2 we need this implementation otherwise we use NSCache directly
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0
@implementation DTCache
- (id)init
{
self = [super init];
if (self)
{
if (NSStringFromClass([NSCache class]) != nil)
{
_cacheMode = DTCacheModeModern;
_cache = [[NSCache alloc] init];
}
else
{
_cacheMode = DTCacheModeLegacy;
_cache = [[NSMutableDictionary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
}
return self;
}
- (void)dealloc
{
if (_cacheMode == DTCacheModeLegacy)
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
[_cache release];
[super dealloc];
}
#pragma mark Passthrough Methods
// methods common on NSCache/NSMutableDictionary
- (id)objectForKey:(id)key
{
return [_cache objectForKey:key];
}
- (void)setObject:(id)obj forKey:(id)key
{
[_cache setObject:obj forKey:key];
}
- (void)removeAllObjects
{
[_cache removeAllObjects];
}
#pragma mark Memory
- (void)didReceiveMemoryWarning
{
// empty cache
[self removeAllObjects];
}
@end
#endif