-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDMemCache.m
81 lines (67 loc) · 1.46 KB
/
PDMemCache.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
//
// PDMemCache.m
// Geospike
//
// Created by Peter Denniss on 8/02/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "PDMemCache.h"
static PDMemCache* sharedInstance = nil;
@implementation PDMemCache
- (id) init
{
self = [super init];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearCache) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearCache) name:UIApplicationDidEnterBackgroundNotification object:nil];
cache = [NSMutableDictionary new];
}
return self;
}
- (void) clearCache
{
[cache removeAllObjects];
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
+ (PDMemCache*) sharedCache
{
@synchronized (self) {
if (sharedInstance == nil){
[self new];
}
}
return sharedInstance;
}
- (void) setObject:(id)object forPath:(NSString *)path
{
if (object != nil)
{
[cache setObject:object forKey:path];
}
}
- (id) objectForPath:(NSString *)path
{
if (path)
{
return [cache objectForKey:path];
}
else
{
return nil;
}
}
@end