-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathCategoryCache.m
198 lines (159 loc) · 7.41 KB
/
CategoryCache.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
/*
Copyright (C) 2017 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
Simple LRU (least recently used) cache for Category objects to reduce fetching.
*/
#import "CategoryCache.h"
#import "Category.h"
// CacheNode is a simple object to help with tracking cached items
//
@interface CacheNode : NSObject {
NSManagedObjectID *objectID;
NSUInteger accessCounter;
}
@property (nonatomic, strong) NSManagedObjectID *objectID;
@property NSUInteger accessCounter;
@end
#pragma mark -
@interface CategoryCache ()
@property (nonatomic, strong, readonly) NSEntityDescription *categoryEntityDescription;
@property (nonatomic, strong, readonly) NSPredicate *categoryNamePredicateTemplate;
// Number of objects that can be cached
@property NSUInteger cacheSize;
// A dictionary holds the actual cached items
@property (nonatomic, strong) NSMutableDictionary *cache;
// Counter used to determine the least recently touched item.
@property (assign) NSUInteger accessCounter;
// Some basic metrics are tracked to help determine the optimal cache size for the problem.
@property (assign) CGFloat totalCacheHitCost;
@property (assign) CGFloat totalCacheMissCost;
@property (assign) NSUInteger cacheHitCount;
@property (assign) NSUInteger cacheMissCount;
@end
@implementation CacheNode
@synthesize objectID, accessCounter;
@end
#pragma mark -
@implementation CategoryCache
@synthesize managedObjectContext, cacheSize, cache, categoryEntityDescription, categoryNamePredicateTemplate;
- (instancetype)init {
self = [super init];
if (self != nil) {
cacheSize = 15;
_accessCounter = 0;
cache = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (self.cacheHitCount > 0) NSLog(@"average cache hit cost: %f", self.totalCacheHitCost/self.cacheHitCount);
if (self.cacheMissCount > 0) NSLog(@"average cache miss cost: %f", self.totalCacheMissCost/self.cacheMissCount);
}
// Implement the "set" accessor rather than depending on @synthesize so that we can set up registration
// for context save notifications.
- (void)setManagedObjectContext:(NSManagedObjectContext *)aContext {
if (managedObjectContext) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];
}
managedObjectContext = aContext;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];
}
// When a managed object is first created, it has a temporary managed object ID. When the managed object context in which it was created is saved, the temporary ID is replaced with a permanent ID. The temporary IDs can no longer be used to retrieve valid managed objects. The cache handles the save notification by iterating through its cache nodes and removing any nodes with temporary IDs.
// While it is possible force Core Data to provide a permanent ID before an object is saved, using the method -[ NSManagedObjectContext obtainPermanentIDsForObjects:error:], this method incurrs a trip to the database, resulting in degraded performance - the very thing we are trying to avoid.
- (void)managedObjectContextDidSave:(NSNotification *)notification {
CacheNode *cacheNode = nil;
NSMutableArray *keys = [NSMutableArray array];
for (NSString *key in cache) {
cacheNode = cache[key];
if (cacheNode.objectID.temporaryID) {
[keys addObject:key];
}
}
[cache removeObjectsForKeys:keys];
}
- (NSEntityDescription *)categoryEntityDescription {
if (categoryEntityDescription == nil) {
categoryEntityDescription = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:managedObjectContext];
}
return categoryEntityDescription;
}
static NSString * const kCategoryNameSubstitutionVariable = @"NAME";
- (NSPredicate *)categoryNamePredicateTemplate {
if (categoryNamePredicateTemplate == nil) {
NSExpression *leftHand = [NSExpression expressionForKeyPath:@"name"];
NSExpression *rightHand = [NSExpression expressionForVariable:kCategoryNameSubstitutionVariable];
categoryNamePredicateTemplate = [[NSComparisonPredicate alloc] initWithLeftExpression:leftHand rightExpression:rightHand modifier:NSDirectPredicateModifier type:NSLikePredicateOperatorType options:0];
}
return categoryNamePredicateTemplate;
}
// Undefine this macro to compare performance without caching.
#define USE_CACHING
- (Category *)categoryWithName:(NSString *)name {
NSTimeInterval before = [NSDate timeIntervalSinceReferenceDate];
#ifdef USE_CACHING
// Check cache.
CacheNode *cacheNode = cache[name];
if (cacheNode != nil) {
// Cache hit, update access counter.
cacheNode.accessCounter = _accessCounter++;
Category *category = (Category *)[managedObjectContext objectWithID:cacheNode.objectID];
_totalCacheHitCost += ([NSDate timeIntervalSinceReferenceDate] - before);
_cacheHitCount++;
return category;
}
#endif
// Cache missed, fetch from store -
// if not found in store there is no category object for the name and we must create one.
//
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = self.categoryEntityDescription;
NSPredicate *predicate = [self.categoryNamePredicateTemplate predicateWithSubstitutionVariables:@{kCategoryNameSubstitutionVariable: name}];
fetchRequest.predicate = predicate;
NSError *error = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSAssert1(fetchResults != nil, @"Unhandled error executing fetch request in import thread: %@", [error localizedDescription]);
Category *category = nil;
if (fetchResults.count > 0) {
// Get category from fetch.
category = fetchResults[0];
} else if (fetchResults.count == 0) {
// Category not in store, must create a new category object.
category =
[[Category alloc] initWithEntity:self.categoryEntityDescription
insertIntoManagedObjectContext:managedObjectContext];
category.name = name;
}
#ifdef USE_CACHING
// Add to cache.
// First check to see if cache is full.
if (cache.count >= cacheSize) {
// Evict least recently used (LRU) item from cache.
NSUInteger oldestAccessCount = UINT_MAX;
NSString *key = nil, *keyOfOldestCacheNode = nil;
for (key in cache) {
CacheNode *tmpNode = cache[key];
if (tmpNode.accessCounter < oldestAccessCount) {
oldestAccessCount = tmpNode.accessCounter;
keyOfOldestCacheNode = key;
}
}
// Retain the cache node for reuse.
cacheNode = cache[keyOfOldestCacheNode];
// Remove from the cache.
if (keyOfOldestCacheNode != nil)
[cache removeObjectForKey:keyOfOldestCacheNode];
} else {
// Create a new cache node.
cacheNode = [[CacheNode alloc] init];
}
cacheNode.objectID = category.objectID;
cacheNode.accessCounter = _accessCounter++;
cache[name] = cacheNode;
#endif
_totalCacheMissCost += ([NSDate timeIntervalSinceReferenceDate] - before);
_cacheMissCount++;
return category;
}
@end