Oolite
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 152 of file OOCache.m.

198{
199 CHECK_INTEGRITY(@"dealloc");
201
202 [super dealloc];
203}
static void CacheFree(OOCacheImpl *cache)
Definition OOCache.m:451
#define CHECK_INTEGRITY(context)
Definition OOCache.m:183
struct OOCacheImpl * cache
Definition OOCache.h:60

◆ description

- (NSString *) description
implementation

Definition at line 152 of file OOCache.m.

207{
208 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"];
209}

◆ dirty

- (BOOL) dirty

◆ init

- (id) init

Definition at line 152 of file OOCache.m.

213{
214 return [self initWithPList:nil];
215}

◆ initWithPList:

- (id) initWithPList: (id)  pList

Definition at line 152 of file OOCache.m.

218 :(id)pList
219{
220 BOOL OK = YES;
221
222 self = [super init];
223 OK = self != nil;
224
225 if (OK)
226 {
228 if (cache == NULL) OK = NO;
229 }
230
231 if (pList != nil)
232 {
233 if (OK) OK = [pList isKindOfClass:[NSArray class]];
234 if (OK) [self loadFromArray:pList];
235 }
236 if (OK)
237 {
239 autoPrune = YES;
240 }
241
242 if (!OK)
243 {
244 [self release];
245 self = nil;
246 }
247
248 return self;
249}
@ 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 152 of file OOCache.m.

391 :(NSArray *)array
392{
393 NSDictionary *entry = nil;
394 NSString *key = nil;
395 id value = nil;
396
397 if (array == nil) return;
398
399 foreach (entry, array)
400 {
401 if ([entry isKindOfClass:[NSDictionary class]])
402 {
403 key = [entry objectForKey:kSerializedEntryKeyKey];
404 value = [entry objectForKey:kSerializedEntryKeyValue];
405 if ([key isKindOfClass:[NSString class]] && value != nil)
406 {
407 [self setObject:value forKey:key];
408 }
409 }
410 }
411}

◆ markClean

- (void) markClean

Definition at line 152 of file OOCache.m.

364{
365 dirty = NO;
366}
BOOL dirty
Definition OOCache.h:63

◆ name

- (NSString *) name

Definition at line 152 of file OOCache.m.

370{
371 return CacheGetName(cache);
372}
static NSString * CacheGetName(OOCacheImpl *cache)
Definition OOCache.m:567

◆ objectForKey:

- (id) objectForKey: (id)  key

Definition at line 152 of file OOCache.m.

260 :(id)key
261{
262 id result = nil;
263
264 CHECK_INTEGRITY(@"objectForKey: before");
265
266 result = CacheRetrieve(cache, key);
267 // 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.
268
269 CHECK_INTEGRITY(@"objectForKey: after");
270
271 return [[result retain] autorelease];
272}
static id CacheRetrieve(OOCacheImpl *cache, id key)
Definition OOCache.m:516

◆ objectsByAge

- (NSArray *) objectsByAge

Definition at line 152 of file OOCache.m.

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

◆ pListRepresentation

- (id) pListRepresentation

Definition at line 152 of file OOCache.m.

253{
255
256 return nil;
257}
static NSArray * CacheArrayOfNodesByAge(OOCacheImpl *cache)
Definition OOCache.m:550

◆ prune

- (void) prune

Definition at line 152 of file OOCache.m.

334{
335 unsigned pruneCount;
336 unsigned desiredCount;
337 unsigned count;
338
339 // Order of operations is to ensure rounding down.
340 if (autoPrune) desiredCount = (pruneThreshold * 4) / 5;
341 else desiredCount = pruneThreshold;
342
344
345 pruneCount = count - desiredCount;
346
347 NSString *logKey = [NSString stringWithFormat:@"dataCache.prune.%@", CacheGetName(cache)];
348 OOLog(logKey, @"Pruning cache \"%@\" - removing %u entries", CacheGetName(cache), pruneCount);
349 OOLogIndentIf(logKey);
350
351 while (pruneCount--) CacheRemoveOldest(cache, logKey);
352
353 OOLogOutdentIf(logKey);
354}
@ 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 152 of file OOCache.m.

289 :(id)key
290{
291 CHECK_INTEGRITY(@"removeObjectForKey: before");
292
293 if (CacheRemove(cache, key)) dirty = YES;
294
295 CHECK_INTEGRITY(@"removeObjectForKey: after");
296}
static BOOL CacheRemove(OOCacheImpl *cache, id key)
Definition OOCache.m:477

◆ setAutoPrune:

- (void) setAutoPrune: (BOOL)  flag

Definition at line 152 of file OOCache.m.

316 :(BOOL)flag
317{
318 BOOL prune = (flag != NO);
319 if (prune != autoPrune)
320 {
322 [self prune];
323 }
324}
void prune()
Definition OOCache.m:333

◆ setName:

- (void) setName: (NSString *)  name

Definition at line 152 of file OOCache.m.

375 :(NSString *)name
376{
378}
static void CacheSetName(OOCacheImpl *cache, NSString *name)
Definition OOCache.m:573
NSString * name()
Definition OOCache.m:369

◆ setObject:forKey:

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

Definition at line 152 of file OOCache.m.

275 :inObject forKey:(id)key
276{
277 CHECK_INTEGRITY(@"setObject:forKey: before");
278
279 if (CacheInsert(cache, key, inObject))
280 {
281 dirty = YES;
282 if (autoPrune) [self prune];
283 }
284
285 CHECK_INTEGRITY(@"setObject:forKey: after");
286}
static BOOL CacheInsert(OOCacheImpl *cache, id key, id value)
Definition OOCache.m:461

◆ setPruneThreshold:

- (void) setPruneThreshold: (unsigned)  threshold

Definition at line 152 of file OOCache.m.

299 :(unsigned)threshold
300{
301 threshold = MAX(threshold, (unsigned)kOOCacheMinimumPruneThreshold);
302 if (threshold != pruneThreshold)
303 {
304 pruneThreshold = threshold;
305 if (autoPrune) [self prune];
306 }
307}
@ 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: