forked from jverkoey/nimbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_n_i_in_memory_cache_8m_source.html
494 lines (476 loc) · 41.8 KB
/
_n_i_in_memory_cache_8m_source.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Nimbus: /Users/featherless/workbench/ios/nimbus/src/core/src/NIInMemoryCache.m Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24278774-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="page">
<div style="position:relative">
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<a style="float: right;margin-right:20px;margin-top:20px" href='http://www.pledgie.com/campaigns/15519'><img alt='Click here to support Nimbus development and make a donation at www.pledgie.com !' src='http://www.pledgie.com/campaigns/15519.png?skin_name=chrome' border='0' /></a>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">Nimbus
 <span id="projectnumber">0.6.0 - <a href="http://github.com/jverkoey/nimbus">Nimbus is proudly hosted on Github</a></span>
</div>
<div id="projectbrief">An iOS framework whose growth is bounded by O(documentation).</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.4-20110629 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
initNavTree('_n_i_in_memory_cache_8m.html','');
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">/Users/featherless/workbench/ios/nimbus/src/core/src/NIInMemoryCache.m</div> </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//</span>
<a name="l00002"></a>00002 <span class="comment">// Copyright 2011 Jeff Verkoeyen</span>
<a name="l00003"></a>00003 <span class="comment">//</span>
<a name="l00004"></a>00004 <span class="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span>
<a name="l00005"></a>00005 <span class="comment">// you may not use this file except in compliance with the License.</span>
<a name="l00006"></a>00006 <span class="comment">// You may obtain a copy of the License at</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// http://www.apache.org/licenses/LICENSE-2.0</span>
<a name="l00009"></a>00009 <span class="comment">//</span>
<a name="l00010"></a>00010 <span class="comment">// Unless required by applicable law or agreed to in writing, software</span>
<a name="l00011"></a>00011 <span class="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span>
<a name="l00012"></a>00012 <span class="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<a name="l00013"></a>00013 <span class="comment">// See the License for the specific language governing permissions and</span>
<a name="l00014"></a>00014 <span class="comment">// limitations under the License.</span>
<a name="l00015"></a>00015 <span class="comment">//</span>
<a name="l00016"></a>00016
<a name="l00017"></a>00017 <span class="preprocessor">#import "NIInMemoryCache.h"</span>
<a name="l00018"></a>00018
<a name="l00019"></a>00019 <span class="preprocessor">#import "NIDataStructures.h"</span>
<a name="l00020"></a>00020 <span class="preprocessor">#import "NIDebuggingTools.h"</span>
<a name="l00021"></a>00021 <span class="preprocessor">#import "NIPreprocessorMacros.h"</span>
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="keyword">@interface </span><a class="code" href="interface_n_i_memory_cache.html" title="An in-memory cache for storing objects with expiration support.">NIMemoryCache</a>()
<a name="l00024"></a>00024
<a name="l00025"></a>00025 <span class="keyword">@property</span> (nonatomic, readwrite, retain) NSMutableDictionary* cacheMap;
<a name="l00026"></a>00026 <span class="keyword">@property</span> (nonatomic, readwrite, retain) <a class="code" href="interface_n_i_linked_list.html" title="A singly linked list implementation.">NILinkedList</a>* lruCacheObjects;
<a name="l00027"></a>00027
<a name="l00028"></a>00028 <span class="keyword">@end</span>
<a name="l00029"></a>00029
<a name="l00030"></a>00030
<a name="l00036"></a><a class="code" href="interface_n_i_memory_cache_info.html">00036</a> <span class="keyword">@interface </span><a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a> : NSObject {
<a name="l00037"></a>00037 <span class="keyword">@private</span>
<a name="l00038"></a>00038 <a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a>* _name;
<a name="l00039"></a>00039 <span class="keywordtype">id</span> _object;
<a name="l00040"></a>00040 NSDate* _expirationDate;
<a name="l00041"></a>00041 NSDate* _lastAccessTime;
<a name="l00042"></a>00042
<a name="l00043"></a>00043 <span class="comment">// Keep tabs on the location of the lru object so that we can move it quickly.</span>
<a name="l00044"></a>00044 NILinkedListLocation* _lruLocation;
<a name="l00045"></a>00045 }
<a name="l00046"></a>00046
<a name="l00050"></a><a class="code" href="interface_n_i_memory_cache_info.html#aacf9857f5788e5a989d0ce9b446a32ee">00050</a> <span class="keyword">@property</span> (nonatomic, readwrite, copy) <a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a>* name;
<a name="l00051"></a>00051
<a name="l00055"></a><a class="code" href="interface_n_i_memory_cache_info.html#aed8c006ce8d5d959170b0c0b8c2c830d">00055</a> <span class="keyword">@property</span> (nonatomic, readwrite, retain) <span class="keywordtype">id</span> <span class="keywordtype">object</span>;
<a name="l00056"></a>00056
<a name="l00060"></a><a class="code" href="interface_n_i_memory_cache_info.html#aae1dd44b6572518d4386b24911407865">00060</a> <span class="keyword">@property</span> (nonatomic, readwrite, retain) NSDate* expirationDate;
<a name="l00061"></a>00061
<a name="l00070"></a><a class="code" href="interface_n_i_memory_cache_info.html#a5df65400aecde4270f65396746022494">00070</a> <span class="keyword">@property</span> (nonatomic, readwrite, retain) NSDate* lastAccessTime;
<a name="l00071"></a>00071
<a name="l00075"></a><a class="code" href="interface_n_i_memory_cache_info.html#a9ba696a78128653d7d2f5208416d6bb0">00075</a> <span class="keyword">@property</span> (nonatomic, readwrite, assign) NILinkedListLocation* lruLocation;
<a name="l00076"></a>00076
<a name="l00084"></a>00084 - (BOOL)hasExpired;
<a name="l00085"></a>00085
<a name="l00086"></a>00086 <span class="keyword">@end</span>
<a name="l00087"></a>00087
<a name="l00088"></a>00088
<a name="l00092"></a>00092 <span class="keyword">@implementation </span><a class="code" href="interface_n_i_memory_cache.html" title="An in-memory cache for storing objects with expiration support.">NIMemoryCache</a>
<a name="l00093"></a>00093
<a name="l00094"></a>00094 <span class="keyword">@synthesize</span> cacheMap = _cacheMap;
<a name="l00095"></a>00095 <span class="keyword">@synthesize</span> lruCacheObjects = _lruCacheObjects;
<a name="l00096"></a>00096
<a name="l00097"></a>00097
<a name="l00099"></a>00099 - (void)dealloc {
<a name="l00100"></a>00100 [[NSNotificationCenter defaultCenter] removeObserver:self];
<a name="l00101"></a>00101
<a name="l00102"></a>00102 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_cacheMap);
<a name="l00103"></a>00103 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_lruCacheObjects);
<a name="l00104"></a>00104
<a name="l00105"></a>00105 [<span class="keyword">super</span> dealloc];
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00108"></a>00108
<a name="l00110"></a>00110 - (id)init {
<a name="l00111"></a>00111 <span class="keywordflow">return</span> [<span class="keyword">self</span> initWithCapacity:0];
<a name="l00112"></a>00112 }
<a name="l00113"></a>00113
<a name="l00114"></a>00114
<a name="l00116"></a><a class="code" href="interface_n_i_memory_cache.html#a879b58be06ace929185997a92f7bb35b">00116</a> - (id)initWithCapacity:(NSUInteger)capacity {
<a name="l00117"></a>00117 <span class="keywordflow">if</span> ((<span class="keyword">self</span> = [super init])) {
<a name="l00118"></a>00118 _cacheMap = [[NSMutableDictionary alloc] initWithCapacity:capacity];
<a name="l00119"></a>00119 _lruCacheObjects = [[<a class="code" href="interface_n_i_linked_list.html" title="A singly linked list implementation.">NILinkedList</a> alloc] init];
<a name="l00120"></a>00120
<a name="l00121"></a>00121 <span class="comment">// Automatically reduce memory usage when we get a memory warning.</span>
<a name="l00122"></a>00122 [[NSNotificationCenter defaultCenter] addObserver: self
<a name="l00123"></a>00123 selector: @selector(reduceMemoryUsage)
<a name="l00124"></a>00124 name: UIApplicationDidReceiveMemoryWarningNotification
<a name="l00125"></a>00125 object: nil];
<a name="l00126"></a>00126 }
<a name="l00127"></a>00127 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00128"></a>00128 }
<a name="l00129"></a>00129
<a name="l00130"></a>00130
<a name="l00133"></a>00133 <span class="preprocessor">#pragma mark -</span>
<a name="l00134"></a>00134 <span class="preprocessor"></span><span class="preprocessor">#pragma mark Internal Methods</span>
<a name="l00135"></a>00135 <span class="preprocessor"></span>
<a name="l00136"></a>00136
<a name="l00138"></a>00138 - (void)updateAccessTimeForInfo:(<a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a> *)info {
<a name="l00139"></a>00139 <a class="code" href="group___debugging-_tools.html#ga13cc3ae82820ea8141bc319bcb76ab7f" title="Assertions that only fire when DEBUG is defined.">NIDASSERT</a>(nil != info);
<a name="l00140"></a>00140 <span class="keywordflow">if</span> (nil == info) {
<a name="l00141"></a>00141 <span class="keywordflow">return</span>;
<a name="l00142"></a>00142 }
<a name="l00143"></a>00143 info.<a class="code" href="interface_n_i_memory_cache_info.html#a5df65400aecde4270f65396746022494" title="The last time this image was accessed.">lastAccessTime</a> = [NSDate date];
<a name="l00144"></a>00144
<a name="l00145"></a>00145 [_lruCacheObjects removeObjectAtLocation:info.lruLocation];
<a name="l00146"></a>00146 info.<a class="code" href="interface_n_i_memory_cache_info.html#a9ba696a78128653d7d2f5208416d6bb0" title="The location of this object in the least-recently used linked list.">lruLocation</a> = [_lruCacheObjects addObject:info];
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148
<a name="l00149"></a>00149
<a name="l00151"></a>00151 - (<a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a> *)cacheInfoForName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00152"></a>00152 <span class="keywordflow">return</span> [_cacheMap objectForKey:name];
<a name="l00153"></a>00153 }
<a name="l00154"></a>00154
<a name="l00155"></a>00155
<a name="l00157"></a>00157 - (void)setCacheInfo:(<a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a> *)info forName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00158"></a>00158 <a class="code" href="group___debugging-_tools.html#ga13cc3ae82820ea8141bc319bcb76ab7f" title="Assertions that only fire when DEBUG is defined.">NIDASSERT</a>(nil != name);
<a name="l00159"></a>00159 <span class="keywordflow">if</span> (nil == name) {
<a name="l00160"></a>00160 <span class="keywordflow">return</span>;
<a name="l00161"></a>00161 }
<a name="l00162"></a>00162
<a name="l00163"></a>00163 <span class="comment">// Storing in the cache counts as an access of the object, so we update the access time.</span>
<a name="l00164"></a>00164 [<span class="keyword">self</span> updateAccessTimeForInfo:info];
<a name="l00165"></a>00165
<a name="l00166"></a>00166 [<span class="keyword">self</span> willSetObject: info.object
<a name="l00167"></a>00167 withName: name
<a name="l00168"></a>00168 previousObject: [<span class="keyword">self</span> cacheInfoForName:name].object];
<a name="l00169"></a>00169
<a name="l00170"></a>00170 [_cacheMap setObject:info forKey:name];
<a name="l00171"></a>00171
<a name="l00172"></a>00172 [<span class="keyword">self</span> didSetObject: info.object
<a name="l00173"></a>00173 withName: name];
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175
<a name="l00176"></a>00176
<a name="l00178"></a>00178 - (void)removeCacheInfoForName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00179"></a>00179 <a class="code" href="group___debugging-_tools.html#ga13cc3ae82820ea8141bc319bcb76ab7f" title="Assertions that only fire when DEBUG is defined.">NIDASSERT</a>(nil != name);
<a name="l00180"></a>00180 <span class="keywordflow">if</span> (nil == name) {
<a name="l00181"></a>00181 <span class="keywordflow">return</span>;
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183
<a name="l00184"></a>00184 [<span class="keyword">self</span> willRemoveObject:[<span class="keyword">self</span> cacheInfoForName:name].object withName:name];
<a name="l00185"></a>00185
<a name="l00186"></a>00186 [_cacheMap removeObjectForKey:name];
<a name="l00187"></a>00187 }
<a name="l00188"></a>00188
<a name="l00189"></a>00189
<a name="l00192"></a>00192 <span class="preprocessor">#pragma mark -</span>
<a name="l00193"></a>00193 <span class="preprocessor"></span><span class="preprocessor">#pragma mark Subclassing</span>
<a name="l00194"></a>00194 <span class="preprocessor"></span>
<a name="l00195"></a>00195
<a name="l00197"></a><a class="code" href="interface_n_i_memory_cache.html#ae1aac8c4de4609814f7f24440cca011f">00197</a> - (void)willSetObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name previousObject:(<span class="keywordtype">id</span>)previousObject {
<a name="l00198"></a>00198 <span class="comment">// No-op</span>
<a name="l00199"></a>00199 }
<a name="l00200"></a>00200
<a name="l00201"></a>00201
<a name="l00203"></a><a class="code" href="interface_n_i_memory_cache.html#ad803d84fcd1c29cf2dadb58c3f719923">00203</a> - (void)didSetObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00204"></a>00204 <span class="comment">// No-op</span>
<a name="l00205"></a>00205 }
<a name="l00206"></a>00206
<a name="l00207"></a>00207
<a name="l00209"></a><a class="code" href="interface_n_i_memory_cache.html#a07a3aa62d34f2cae0c4c76668de9608e">00209</a> - (void)willRemoveObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00210"></a>00210 <span class="comment">// No-op</span>
<a name="l00211"></a>00211 }
<a name="l00212"></a>00212
<a name="l00213"></a>00213
<a name="l00216"></a>00216 <span class="preprocessor">#pragma mark -</span>
<a name="l00217"></a>00217 <span class="preprocessor"></span><span class="preprocessor">#pragma mark Public Methods</span>
<a name="l00218"></a>00218 <span class="preprocessor"></span>
<a name="l00219"></a>00219
<a name="l00221"></a><a class="code" href="interface_n_i_memory_cache.html#a7bc119332422cd4d36863a5f1d7751bd">00221</a> - (void)storeObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00222"></a>00222 [<span class="keyword">self</span> <a class="code" href="interface_n_i_memory_cache.html#acaa6af8abea20907cf230ce1191e7ad4" title="Stores an object in the cache with an expiration date.">storeObject</a>:object <a class="code" href="interface_n_i_memory_cache.html#acaa6af8abea20907cf230ce1191e7ad4" title="Stores an object in the cache with an expiration date.">withName</a>:name <a class="code" href="interface_n_i_memory_cache.html#acaa6af8abea20907cf230ce1191e7ad4" title="Stores an object in the cache with an expiration date.">expiresAfter</a>:nil];
<a name="l00223"></a>00223 }
<a name="l00224"></a>00224
<a name="l00225"></a>00225
<a name="l00227"></a><a class="code" href="interface_n_i_memory_cache.html#acaa6af8abea20907cf230ce1191e7ad4">00227</a> - (void)storeObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name expiresAfter:(NSDate *)expirationDate {
<a name="l00228"></a>00228 <span class="comment">// Don't store nil objects in the cache.</span>
<a name="l00229"></a>00229 <span class="keywordflow">if</span> (nil == <span class="keywordtype">object</span>) {
<a name="l00230"></a>00230 <span class="keywordflow">return</span>;
<a name="l00231"></a>00231 }
<a name="l00232"></a>00232
<a name="l00233"></a>00233 <span class="keywordflow">if</span> (nil != expirationDate && [[NSDate date] timeIntervalSinceDate:expirationDate] >= 0) {
<a name="l00234"></a>00234 <span class="comment">// The object being stored is already expired so remove the object from the cache altogether.</span>
<a name="l00235"></a>00235 [<span class="keyword">self</span> <a class="code" href="interface_n_i_memory_cache.html#a5dab1798d4e3bcd2b686837f63f31d1c" title="Removes an object from the cache.">removeObjectWithName</a>:name];
<a name="l00236"></a>00236
<a name="l00237"></a>00237 <span class="comment">// We're done here.</span>
<a name="l00238"></a>00238 <span class="keywordflow">return</span>;
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00241"></a>00241
<a name="l00242"></a>00242 <span class="comment">// Create a new cache entry.</span>
<a name="l00243"></a>00243 <span class="keywordflow">if</span> (nil == info) {
<a name="l00244"></a>00244 info = [[[<a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a> alloc] init] autorelease];
<a name="l00245"></a>00245 info.<a class="code" href="interface_n_i_memory_cache_info.html#aacf9857f5788e5a989d0ce9b446a32ee" title="The name used to store this object in the cache.">name</a> = name;
<a name="l00246"></a>00246 }
<a name="l00247"></a>00247
<a name="l00248"></a>00248 <span class="comment">// Store the object in the cache item.</span>
<a name="l00249"></a>00249 info.<a class="code" href="interface_n_i_memory_cache_info.html#aed8c006ce8d5d959170b0c0b8c2c830d" title="The object stored in the cache.">object</a> = object;
<a name="l00250"></a>00250
<a name="l00251"></a>00251 <span class="comment">// Override any existing expiration date.</span>
<a name="l00252"></a>00252 info.<a class="code" href="interface_n_i_memory_cache_info.html#aae1dd44b6572518d4386b24911407865" title="The date after which the image is no longer valid and should be removed from the cache.">expirationDate</a> = expirationDate;
<a name="l00253"></a>00253
<a name="l00254"></a>00254 <span class="comment">// Commit the changes to the cache.</span>
<a name="l00255"></a>00255 [<span class="keyword">self</span> setCacheInfo:info forName:name];
<a name="l00256"></a>00256 }
<a name="l00257"></a>00257
<a name="l00258"></a>00258
<a name="l00260"></a><a class="code" href="interface_n_i_memory_cache.html#afdd7b533b55c5d8753078e207ec0a9d7">00260</a> - (id)objectWithName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00261"></a>00261 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00262"></a>00262
<a name="l00263"></a>00263 <span class="keywordtype">id</span> <span class="keywordtype">object</span> = nil;
<a name="l00264"></a>00264
<a name="l00265"></a>00265 <span class="keywordflow">if</span> (nil != info) {
<a name="l00266"></a>00266 <span class="keywordflow">if</span> ([info hasExpired]) {
<a name="l00267"></a>00267 [<span class="keyword">self</span> <a class="code" href="interface_n_i_memory_cache.html#a5dab1798d4e3bcd2b686837f63f31d1c" title="Removes an object from the cache.">removeObjectWithName</a>:name];
<a name="l00268"></a>00268
<a name="l00269"></a>00269 } <span class="keywordflow">else</span> {
<a name="l00270"></a>00270 <span class="comment">// Update the access time whenever we fetch an object from the cache.</span>
<a name="l00271"></a>00271 [<span class="keyword">self</span> updateAccessTimeForInfo:info];
<a name="l00272"></a>00272
<a name="l00273"></a>00273 <span class="keywordtype">object</span> = info.<a class="code" href="interface_n_i_memory_cache_info.html#aed8c006ce8d5d959170b0c0b8c2c830d" title="The object stored in the cache.">object</a>;
<a name="l00274"></a>00274 }
<a name="l00275"></a>00275 }
<a name="l00276"></a>00276
<a name="l00277"></a>00277 <span class="keywordflow">return</span> [[object retain] autorelease];
<a name="l00278"></a>00278 }
<a name="l00279"></a>00279
<a name="l00280"></a>00280
<a name="l00282"></a><a class="code" href="interface_n_i_memory_cache.html#aadf04db4cd1ef2087971ac58b416c590">00282</a> - (BOOL)containsObjectWithName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00283"></a>00283 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00284"></a>00284
<a name="l00285"></a>00285 <span class="keywordflow">if</span> ([info hasExpired]) {
<a name="l00286"></a>00286 [<span class="keyword">self</span> <a class="code" href="interface_n_i_memory_cache.html#a5dab1798d4e3bcd2b686837f63f31d1c" title="Removes an object from the cache.">removeObjectWithName</a>:name];
<a name="l00287"></a>00287 <span class="keywordflow">return</span> NO;
<a name="l00288"></a>00288 }
<a name="l00289"></a>00289
<a name="l00290"></a>00290 <span class="keywordflow">return</span> (nil != info);
<a name="l00291"></a>00291 }
<a name="l00292"></a>00292
<a name="l00293"></a>00293
<a name="l00295"></a><a class="code" href="interface_n_i_memory_cache.html#a6c016c9a322c58151bbfb30793e9d475">00295</a> - (NSDate *)dateOfLastAccessWithName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00296"></a>00296 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00297"></a>00297
<a name="l00298"></a>00298 <span class="keywordflow">if</span> ([info hasExpired]) {
<a name="l00299"></a>00299 [<span class="keyword">self</span> <a class="code" href="interface_n_i_memory_cache.html#a5dab1798d4e3bcd2b686837f63f31d1c" title="Removes an object from the cache.">removeObjectWithName</a>:name];
<a name="l00300"></a>00300 <span class="keywordflow">return</span> nil;
<a name="l00301"></a>00301 }
<a name="l00302"></a>00302
<a name="l00303"></a>00303 <span class="keywordflow">return</span> [info <a class="code" href="interface_n_i_memory_cache_info.html#a5df65400aecde4270f65396746022494" title="The last time this image was accessed.">lastAccessTime</a>];
<a name="l00304"></a>00304 }
<a name="l00305"></a>00305
<a name="l00306"></a>00306
<a name="l00308"></a><a class="code" href="interface_n_i_memory_cache.html#a5dab1798d4e3bcd2b686837f63f31d1c">00308</a> - (void)removeObjectWithName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00309"></a>00309 [<span class="keyword">self</span> removeCacheInfoForName:name];
<a name="l00310"></a>00310 }
<a name="l00311"></a>00311
<a name="l00312"></a>00312
<a name="l00314"></a><a class="code" href="interface_n_i_memory_cache.html#a26fda388dffc22aa78dd17905a6fce73">00314</a> - (void)<a class="code" href="interface_n_i_memory_cache.html#a26fda388dffc22aa78dd17905a6fce73" title="Removes all objects from the cache, regardless of expiration dates.">removeAllObjects</a> {
<a name="l00315"></a>00315 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_cacheMap);
<a name="l00316"></a>00316 _cacheMap = [[NSMutableDictionary alloc] init];
<a name="l00317"></a>00317 }
<a name="l00318"></a>00318
<a name="l00319"></a>00319
<a name="l00321"></a><a class="code" href="interface_n_i_memory_cache.html#a8164a276bcc012398b4025d45fe597d2">00321</a> - (void)<a class="code" href="interface_n_i_memory_cache.html#a8164a276bcc012398b4025d45fe597d2" title="Removes all expired objects from the cache.">reduceMemoryUsage</a> {
<a name="l00322"></a>00322 <span class="comment">// Copy the cache map because it's likely that we're going to modify it.</span>
<a name="l00323"></a>00323 NSDictionary* cacheMap = [_cacheMap copy];
<a name="l00324"></a>00324
<a name="l00325"></a>00325 <span class="comment">// Iterate over the copied cache map (which will not be modified).</span>
<a name="l00326"></a>00326 <span class="keywordflow">for</span> (<span class="keywordtype">id</span> name in cacheMap) {
<a name="l00327"></a>00327 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00328"></a>00328
<a name="l00329"></a>00329 <span class="keywordflow">if</span> ([info hasExpired]) {
<a name="l00330"></a>00330 [<span class="keyword">self</span> removeCacheInfoForName:name];
<a name="l00331"></a>00331 }
<a name="l00332"></a>00332 }
<a name="l00333"></a>00333 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(cacheMap);
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335
<a name="l00336"></a>00336
<a name="l00338"></a><a class="code" href="interface_n_i_memory_cache.html#a61996ab64791ed19840b5055b68c7c95">00338</a> - (NSUInteger)<a class="code" href="interface_n_i_memory_cache.html#a61996ab64791ed19840b5055b68c7c95" title="Returns the number of objects currently in the cache.">count</a> {
<a name="l00339"></a>00339 <span class="keywordflow">return</span> [_cacheMap count];
<a name="l00340"></a>00340 }
<a name="l00341"></a>00341
<a name="l00342"></a>00342
<a name="l00343"></a>00343 <span class="keyword">@end</span>
<a name="l00344"></a>00344
<a name="l00345"></a>00345
<a name="l00349"></a>00349 <span class="keyword">@implementation </span><a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>
<a name="l00350"></a>00350
<a name="l00351"></a>00351 <span class="keyword">@synthesize</span> name = _name;
<a name="l00352"></a>00352 <span class="keyword">@synthesize</span> <span class="keywordtype">object</span> = _object;
<a name="l00353"></a>00353 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_memory_cache_info.html#aae1dd44b6572518d4386b24911407865" title="The date after which the image is no longer valid and should be removed from the cache.">expirationDate</a> = _expirationDate;
<a name="l00354"></a>00354 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_memory_cache_info.html#a5df65400aecde4270f65396746022494" title="The last time this image was accessed.">lastAccessTime</a> = _lastAccessTime;
<a name="l00355"></a>00355 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_memory_cache_info.html#a9ba696a78128653d7d2f5208416d6bb0" title="The location of this object in the least-recently used linked list.">lruLocation</a> = _lruLocation;
<a name="l00356"></a>00356
<a name="l00357"></a>00357
<a name="l00359"></a>00359 - (void)dealloc {
<a name="l00360"></a>00360 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_name);
<a name="l00361"></a>00361 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_object);
<a name="l00362"></a>00362 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_expirationDate);
<a name="l00363"></a>00363 <a class="code" href="group___preprocessor-_macros.html#ga5d2e99251aeb3ddb687e27f72336e5db" title="Release and assign nil to an object.">NI_RELEASE_SAFELY</a>(_lastAccessTime);
<a name="l00364"></a>00364 _lruLocation = nil;
<a name="l00365"></a>00365
<a name="l00366"></a>00366 [<span class="keyword">super</span> dealloc];
<a name="l00367"></a>00367 }
<a name="l00368"></a>00368
<a name="l00369"></a>00369
<a name="l00371"></a><a class="code" href="interface_n_i_memory_cache_info.html#ae854ab244f76213c32704ead39bd3e2a">00371</a> - (BOOL)<a class="code" href="interface_n_i_memory_cache_info.html#ae854ab244f76213c32704ead39bd3e2a" title="Determine whether this cache entry has past its expiration date.">hasExpired</a> {
<a name="l00372"></a>00372 <span class="keywordflow">return</span> (nil != _expirationDate
<a name="l00373"></a>00373 && [[NSDate date] timeIntervalSinceDate:_expirationDate] >= 0);
<a name="l00374"></a>00374 }
<a name="l00375"></a>00375
<a name="l00376"></a>00376
<a name="l00377"></a>00377 <span class="keyword">@end</span>
<a name="l00378"></a>00378
<a name="l00379"></a>00379
<a name="l00383"></a>00383 <span class="keyword">@interface </span><a class="code" href="interface_n_i_image_memory_cache.html" title="An in-memory cache for storing images with caps on the total number of pixels.">NIImageMemoryCache</a>()
<a name="l00384"></a>00384
<a name="l00385"></a>00385 <span class="comment">// Internally only.</span>
<a name="l00386"></a>00386 <span class="keyword">@property</span> (nonatomic, readwrite, assign) NSUInteger <a class="code" href="interface_n_i_image_memory_cache.html#a12b709e0f6f319e76c3dc3d6dbe3ee5a" title="Returns the total number of pixels being stored in the cache.">numberOfPixels</a>;
<a name="l00387"></a>00387
<a name="l00388"></a>00388 <span class="keyword">@end</span>
<a name="l00389"></a>00389
<a name="l00390"></a>00390
<a name="l00394"></a>00394 <span class="keyword">@implementation </span><a class="code" href="interface_n_i_image_memory_cache.html" title="An in-memory cache for storing images with caps on the total number of pixels.">NIImageMemoryCache</a>
<a name="l00395"></a>00395
<a name="l00396"></a>00396 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_image_memory_cache.html#a12b709e0f6f319e76c3dc3d6dbe3ee5a" title="Returns the total number of pixels being stored in the cache.">numberOfPixels</a> = _numberOfPixels;
<a name="l00397"></a>00397 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_image_memory_cache.html#a30bd865b4e57ff8a73fd459114c22aed" title="The maximum number of pixels this cache may ever store.">maxNumberOfPixels</a> = _maxNumberOfPixels;
<a name="l00398"></a>00398 <span class="keyword">@synthesize</span> <a class="code" href="interface_n_i_image_memory_cache.html#a100ad5b6dea102a5ae296dab5830ce50" title="The maximum number of pixels this cache may store after a call to reduceMemoryUsage.">maxNumberOfPixelsUnderStress</a> = _maxNumberOfPixelsUnderStress;
<a name="l00399"></a>00399
<a name="l00400"></a>00400
<a name="l00402"></a>00402 - (NSUInteger)numberOfPixelsUsedByImage:(UIImage *)image {
<a name="l00403"></a>00403 <span class="keywordflow">if</span> (nil == image) {
<a name="l00404"></a>00404 <span class="keywordflow">return</span> 0;
<a name="l00405"></a>00405 }
<a name="l00406"></a>00406
<a name="l00407"></a>00407 NSUInteger <a class="code" href="interface_n_i_image_memory_cache.html#a12b709e0f6f319e76c3dc3d6dbe3ee5a" title="Returns the total number of pixels being stored in the cache.">numberOfPixels</a> = (NSUInteger)(image.size.width * image.size.height);
<a name="l00408"></a>00408 <span class="keywordflow">if</span> ([image respondsToSelector:<span class="keyword">@selector</span>(scale)]) {
<a name="l00409"></a>00409 numberOfPixels *= [image scale];
<a name="l00410"></a>00410 }
<a name="l00411"></a>00411 <span class="keywordflow">return</span> <a class="code" href="interface_n_i_image_memory_cache.html#a12b709e0f6f319e76c3dc3d6dbe3ee5a" title="Returns the total number of pixels being stored in the cache.">numberOfPixels</a>;
<a name="l00412"></a>00412 }
<a name="l00413"></a>00413
<a name="l00414"></a>00414
<a name="l00416"></a>00416 - (void)<a class="code" href="interface_n_i_memory_cache.html#a8164a276bcc012398b4025d45fe597d2" title="Removes all expired objects from the cache.">reduceMemoryUsage</a> {
<a name="l00417"></a>00417 <span class="comment">// Remove all expired images first.</span>
<a name="l00418"></a>00418 [<span class="keyword">super</span> reduceMemoryUsage];
<a name="l00419"></a>00419
<a name="l00420"></a>00420 <span class="keywordflow">if</span> (<span class="keyword">self</span>.<a class="code" href="interface_n_i_image_memory_cache.html#a100ad5b6dea102a5ae296dab5830ce50" title="The maximum number of pixels this cache may store after a call to reduceMemoryUsage.">maxNumberOfPixelsUnderStress</a> > 0) {
<a name="l00421"></a>00421 <span class="comment">// Remove the least recently used images by iterating over the linked list.</span>
<a name="l00422"></a>00422 <span class="keywordflow">while</span> (<span class="keyword">self</span>.numberOfPixels > <span class="keyword">self</span>.<a class="code" href="interface_n_i_image_memory_cache.html#a100ad5b6dea102a5ae296dab5830ce50" title="The maximum number of pixels this cache may store after a call to reduceMemoryUsage.">maxNumberOfPixelsUnderStress</a>) {
<a name="l00423"></a>00423 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span>.lruCacheObjects firstObject];
<a name="l00424"></a>00424 [<span class="keyword">self</span> removeCacheInfoForName:info.name];
<a name="l00425"></a>00425 }
<a name="l00426"></a>00426 }
<a name="l00427"></a>00427 }
<a name="l00428"></a>00428
<a name="l00429"></a>00429
<a name="l00431"></a>00431 - (void)willSetObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name previousObject:(<span class="keywordtype">id</span>)previousObject {
<a name="l00432"></a>00432 <a class="code" href="group___debugging-_tools.html#ga13cc3ae82820ea8141bc319bcb76ab7f" title="Assertions that only fire when DEBUG is defined.">NIDASSERT</a>(nil == <span class="keywordtype">object</span> || [<span class="keywordtype">object</span> isKindOfClass:[UIImage <span class="keyword">class</span>]]);
<a name="l00433"></a>00433 <span class="keywordflow">if</span> (![<span class="keywordtype">object</span> isKindOfClass:[UIImage <span class="keyword">class</span>]]) {
<a name="l00434"></a>00434 <span class="keywordflow">return</span>;
<a name="l00435"></a>00435 }
<a name="l00436"></a>00436
<a name="l00437"></a>00437 <span class="keyword">self</span>.numberOfPixels -= [self numberOfPixelsUsedByImage:previousObject];
<a name="l00438"></a>00438 <span class="keyword">self</span>.numberOfPixels += [self numberOfPixelsUsedByImage:object];
<a name="l00439"></a>00439 }
<a name="l00440"></a>00440
<a name="l00441"></a>00441
<a name="l00443"></a>00443 - (void)didSetObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00444"></a>00444 <span class="comment">// Reduce the cache size after the object has been set in case the cache size is smaller</span>
<a name="l00445"></a>00445 <span class="comment">// than the object that's being added and we need to remove this object right away. If we</span>
<a name="l00446"></a>00446 <span class="comment">// try to reduce the cache size before the object's been set, we won't have anything to remove</span>
<a name="l00447"></a>00447 <span class="comment">// and we'll get stuck in an infinite loop.</span>
<a name="l00448"></a>00448 <span class="keywordflow">if</span> (<span class="keyword">self</span>.<a class="code" href="interface_n_i_image_memory_cache.html#a30bd865b4e57ff8a73fd459114c22aed" title="The maximum number of pixels this cache may ever store.">maxNumberOfPixels</a> > 0) {
<a name="l00449"></a>00449 <span class="comment">// Remove least recently used images until we satisfy our memory constraints.</span>
<a name="l00450"></a>00450 <span class="keywordflow">while</span> (<span class="keyword">self</span>.numberOfPixels > <span class="keyword">self</span>.<a class="code" href="interface_n_i_image_memory_cache.html#a30bd865b4e57ff8a73fd459114c22aed" title="The maximum number of pixels this cache may ever store.">maxNumberOfPixels</a>
<a name="l00451"></a>00451 && [<span class="keyword">self</span>.lruCacheObjects <a class="code" href="interface_n_i_memory_cache.html#a61996ab64791ed19840b5055b68c7c95" title="Returns the number of objects currently in the cache.">count</a>] > 0) {
<a name="l00452"></a>00452 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span>.lruCacheObjects firstObject];
<a name="l00453"></a>00453 [<span class="keyword">self</span> removeCacheInfoForName:info.name];
<a name="l00454"></a>00454 }
<a name="l00455"></a>00455 }
<a name="l00456"></a>00456 }
<a name="l00457"></a>00457
<a name="l00458"></a>00458
<a name="l00460"></a>00460 - (void)willRemoveObject:(<span class="keywordtype">id</span>)object withName:(<a class="code" href="class_n_s_string.html" title="For manipulating NSStrings.">NSString</a> *)name {
<a name="l00461"></a>00461 <a class="code" href="group___debugging-_tools.html#ga13cc3ae82820ea8141bc319bcb76ab7f" title="Assertions that only fire when DEBUG is defined.">NIDASSERT</a>(nil == <span class="keywordtype">object</span> || [<span class="keywordtype">object</span> isKindOfClass:[UIImage <span class="keyword">class</span>]]);
<a name="l00462"></a>00462 <span class="keywordflow">if</span> (nil == <span class="keywordtype">object</span> || ![<span class="keywordtype">object</span> isKindOfClass:[UIImage <span class="keyword">class</span>]]) {
<a name="l00463"></a>00463 <span class="keywordflow">return</span>;
<a name="l00464"></a>00464 }
<a name="l00465"></a>00465
<a name="l00466"></a>00466 <a class="code" href="interface_n_i_memory_cache_info.html" title="A single cache item's information.">NIMemoryCacheInfo</a>* info = [<span class="keyword">self</span> cacheInfoForName:name];
<a name="l00467"></a>00467 [<span class="keyword">self</span>.lruCacheObjects removeObjectAtLocation:info.lruLocation];
<a name="l00468"></a>00468
<a name="l00469"></a>00469 <span class="keyword">self</span>.numberOfPixels -= [self numberOfPixelsUsedByImage:object];
<a name="l00470"></a>00470 }
<a name="l00471"></a>00471
<a name="l00472"></a>00472
<a name="l00473"></a>00473 <span class="keyword">@end</span>
<a name="l00474"></a>00474
</pre></div></div>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><b>NIInMemoryCache.m</b> </li>
</div><!-- .fixedwidth -->
<span class="footer">Generated for Nimbus by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4-20110629 </span>
</ul>
</div>
</div> <!-- page -->
</body>
</html>