Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOCache Class Reference

#include <OOCache.h>

+ Inheritance diagram for OOCache:
+ Collaboration diagram for OOCache:

Instance Methods

(id) - init
 
(id) - initWithPList:
 
(id) - pListRepresentation
 
(id) - objectForKey:
 
(void) - setObject:forKey:
 
(void) - removeObjectForKey:
 
(void) - setPruneThreshold:
 
(unsigned) - pruneThreshold
 
(void) - setAutoPrune:
 
(BOOL) - autoPrune
 
(void) - prune
 
(BOOL) - dirty
 
(void) - markClean
 
(NSString *) - name
 
(void) - setName:
 
(NSArray *) - objectsByAge
 
(void) - dealloc [implementation]
 
(NSString *) - description [implementation]
 
(void) - loadFromArray: [implementation]
 

Private Attributes

struct OOCacheImplcache
 
unsigned pruneThreshold
 
BOOL autoPrune
 
BOOL dirty
 

Detailed Description

Definition at line 57 of file OOCache.h.

Method Documentation

◆ autoPrune

- (BOOL) autoPrune

◆ dealloc

- (void) dealloc
implementation

Definition at line 917 of file OOCache.m.

171{
172 CHECK_INTEGRITY(@"dealloc");
174
175 [super dealloc];
176}
static void CacheFree(OOCacheImpl *cache)
Definition OOCache.m:451
#define CHECK_INTEGRITY(context)
Definition OOCache.m:156
struct OOCacheImpl * cache
Definition OOCache.h:60

◆ description

- (NSString *) description
implementation

Definition at line 917 of file OOCache.m.

180{
181 return [NSString stringWithFormat:@"<%@ %p>{\"%@\", %u elements, prune threshold=%u, auto-prune=%s dirty=%s}", [self class], self, [self name], CacheGetCount(cache), pruneThreshold, autoPrune ? "yes" : "no", dirty ? "yes" : "no"];
182}

◆ dirty

- (BOOL) dirty

◆ init

- (id) init

Definition at line 917 of file OOCache.m.

186{
187 return [self initWithPList:nil];
188}

◆ initWithPList:

- (id) initWithPList: (id) pList

Definition at line 917 of file OOCache.m.

191 :(id)pList
192{
193 BOOL OK = YES;
194
195 self = [super init];
196 OK = self != nil;
197
198 if (OK)
199 {
201 if (cache == NULL) OK = NO;
202 }
203
204 if (pList != nil)
205 {
206 if (OK) OK = [pList isKindOfClass:[NSArray class]];
207 if (OK) [self loadFromArray:pList];
208 }
209 if (OK)
210 {
212 autoPrune = YES;
213 }
214
215 if (!OK)
216 {
217 [self release];
218 self = nil;
219 }
220
221 return self;
222}
@ kOOCacheDefaultPruneThreshold
Definition OOCache.h:52
static OOCacheImpl * CacheAllocate(void)
Definition OOCache.m:445
return nil
BOOL autoPrune
Definition OOCache.h:62
unsigned pruneThreshold
Definition OOCache.h:61

◆ loadFromArray:

- (void) loadFromArray: (NSArray *) inArray
implementation

Provided by category OOCache(Private).

Definition at line 917 of file OOCache.m.

364 :(NSArray *)array
365{
366 NSEnumerator *entryEnum = nil;
367 NSDictionary *entry = nil;
368 NSString *key = nil;
369 id value = nil;
370
371 if (array == nil) return;
372
373 for (entryEnum = [array objectEnumerator]; (entry = [entryEnum nextObject]); )
374 {
375 if ([entry isKindOfClass:[NSDictionary class]])
376 {
377 key = [entry objectForKey:kSerializedEntryKeyKey];
378 value = [entry objectForKey:kSerializedEntryKeyValue];
379 if ([key isKindOfClass:[NSString class]] && value != nil)
380 {
381 [self setObject:value forKey:key];
382 }
383 }
384 }
385}

◆ markClean

- (void) markClean

Definition at line 917 of file OOCache.m.

337{
338 dirty = NO;
339}
BOOL dirty
Definition OOCache.h:63

◆ name

- (NSString *) name

Definition at line 917 of file OOCache.m.

343{
344 return CacheGetName(cache);
345}
static NSString * CacheGetName(OOCacheImpl *cache)
Definition OOCache.m:567

◆ objectForKey:

- (id) objectForKey: (id) key

Definition at line 917 of file OOCache.m.

233 :(id)key
234{
235 id result = nil;
236
237 CHECK_INTEGRITY(@"objectForKey: before");
238
239 result = CacheRetrieve(cache, key);
240 // Note: while reordering the age list technically makes the cache dirty, it's not worth rewriting it just for that, so we don't flag it.
241
242 CHECK_INTEGRITY(@"objectForKey: after");
243
244 return [[result retain] autorelease];
245}
static id CacheRetrieve(OOCacheImpl *cache, id key)
Definition OOCache.m:516

◆ objectsByAge

- (NSArray *) objectsByAge

Definition at line 917 of file OOCache.m.

355{
357}
static NSArray * CacheArrayOfContentsByAge(OOCacheImpl *cache)
Definition OOCache.m:533

◆ pListRepresentation

- (id) pListRepresentation

Definition at line 917 of file OOCache.m.

226{
228
229 return nil;
230}
static NSArray * CacheArrayOfNodesByAge(OOCacheImpl *cache)
Definition OOCache.m:550

◆ prune

- (void) prune

Definition at line 917 of file OOCache.m.

307{
308 unsigned pruneCount;
309 unsigned desiredCount;
310 unsigned count;
311
312 // Order of operations is to ensure rounding down.
313 if (autoPrune) desiredCount = (pruneThreshold * 4) / 5;
314 else desiredCount = pruneThreshold;
315
317
318 pruneCount = count - desiredCount;
319
320 NSString *logKey = [NSString stringWithFormat:@"dataCache.prune.%@", CacheGetName(cache)];
321 OOLog(logKey, @"Pruning cache \"%@\" - removing %u entries", CacheGetName(cache), pruneCount);
322 OOLogIndentIf(logKey);
323
324 while (pruneCount--) CacheRemoveOldest(cache, logKey);
325
326 OOLogOutdentIf(logKey);
327}
@ kOOCacheNoPrune
Definition OOCache.h:53
static unsigned CacheGetCount(OOCacheImpl *cache)
Definition OOCache.m:580
static BOOL CacheRemoveOldest(OOCacheImpl *cache, NSString *logKey)
Definition OOCache.m:506
#define OOLogOutdentIf(class)
Definition OOLogging.h:102
#define OOLog(class, format,...)
Definition OOLogging.h:88
#define OOLogIndentIf(class)
Definition OOLogging.h:101
unsigned count

◆ pruneThreshold

- (unsigned) pruneThreshold

◆ removeObjectForKey:

- (void) removeObjectForKey: (id) key

Definition at line 917 of file OOCache.m.

262 :(id)key
263{
264 CHECK_INTEGRITY(@"removeObjectForKey: before");
265
266 if (CacheRemove(cache, key)) dirty = YES;
267
268 CHECK_INTEGRITY(@"removeObjectForKey: after");
269}
static BOOL CacheRemove(OOCacheImpl *cache, id key)
Definition OOCache.m:477

◆ setAutoPrune:

- (void) setAutoPrune: (BOOL) flag

Definition at line 917 of file OOCache.m.

289 :(BOOL)flag
290{
291 BOOL prune = (flag != NO);
292 if (prune != autoPrune)
293 {
295 [self prune];
296 }
297}
void prune()
Definition OOCache.m:306

◆ setName:

- (void) setName: (NSString *) name

Definition at line 917 of file OOCache.m.

348 :(NSString *)name
349{
351}
static void CacheSetName(OOCacheImpl *cache, NSString *name)
Definition OOCache.m:573
NSString * name()
Definition OOCache.m:342

◆ setObject:forKey:

- (void) setObject: (id) value
forKey: (id) key 

Definition at line 917 of file OOCache.m.

248 :inObject forKey:(id)key
249{
250 CHECK_INTEGRITY(@"setObject:forKey: before");
251
252 if (CacheInsert(cache, key, inObject))
253 {
254 dirty = YES;
255 if (autoPrune) [self prune];
256 }
257
258 CHECK_INTEGRITY(@"setObject:forKey: after");
259}
static BOOL CacheInsert(OOCacheImpl *cache, id key, id value)
Definition OOCache.m:461

◆ setPruneThreshold:

- (void) setPruneThreshold: (unsigned) threshold

Definition at line 917 of file OOCache.m.

272 :(unsigned)threshold
273{
274 threshold = MAX(threshold, (unsigned)kOOCacheMinimumPruneThreshold);
275 if (threshold != pruneThreshold)
276 {
277 pruneThreshold = threshold;
278 if (autoPrune) [self prune];
279 }
280}
@ kOOCacheMinimumPruneThreshold
Definition OOCache.h:51
#define MAX(A, B)
Definition OOMaths.h:114

Member Data Documentation

◆ autoPrune

- (BOOL) autoPrune
private

Definition at line 62 of file OOCache.h.

◆ cache

- (struct OOCacheImpl*) cache
private

Definition at line 60 of file OOCache.h.

◆ dirty

- (BOOL) dirty
private

Definition at line 63 of file OOCache.h.

◆ pruneThreshold

- (unsigned) pruneThreshold
private

Definition at line 61 of file OOCache.h.


The documentation for this class was generated from the following files: