Skip to content

Commit

Permalink
added configurable cache expiration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Apr 13, 2012
1 parent 6e5efaa commit 037667b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions MapView/Map/RMDatabaseCache.h
Expand Up @@ -43,5 +43,6 @@
- (void)setPurgeStrategy:(RMCachePurgeStrategy)theStrategy; - (void)setPurgeStrategy:(RMCachePurgeStrategy)theStrategy;
- (void)setCapacity:(NSUInteger)theCapacity; - (void)setCapacity:(NSUInteger)theCapacity;
- (void)setMinimalPurge:(NSUInteger)thePurgeMinimum; - (void)setMinimalPurge:(NSUInteger)thePurgeMinimum;
- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod;


@end @end
32 changes: 31 additions & 1 deletion MapView/Map/RMDatabaseCache.m
Expand Up @@ -57,6 +57,7 @@ @implementation RMDatabaseCache
RMCachePurgeStrategy purgeStrategy; RMCachePurgeStrategy purgeStrategy;
NSUInteger capacity; NSUInteger capacity;
NSUInteger minimalPurge; NSUInteger minimalPurge;
NSTimeInterval expiryPeriod;
} }


@synthesize databasePath; @synthesize databasePath;
Expand Down Expand Up @@ -169,6 +170,13 @@ - (void)setMinimalPurge:(NSUInteger)theMinimalPurge
minimalPurge = theMinimalPurge; minimalPurge = theMinimalPurge;
} }


- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod
{
expiryPeriod = theExpiryPeriod;

srand(time(NULL));
}

- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{ {
// RMLog(@"DB cache check for tile %d %d %d", tile.x, tile.y, tile.zoom); // RMLog(@"DB cache check for tile %d %d %d", tile.x, tile.y, tile.zoom);
Expand Down Expand Up @@ -203,6 +211,28 @@ - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
if (capacity != 0 && purgeStrategy == RMCachePurgeStrategyLRU) if (capacity != 0 && purgeStrategy == RMCachePurgeStrategyLRU)
[self touchTile:tile withKey:aCacheKey]; [self touchTile:tile withKey:aCacheKey];


if (expiryPeriod > 0)
{
if (rand() % 100 == 0)
{
[writeQueueLock lock];

[queue inDatabase:^(FMDatabase *db)
{
BOOL result = [db executeUpdate:@"DELETE FROM ZCACHE WHERE last_used < ?", [NSDate dateWithTimeIntervalSinceNow:-expiryPeriod]];

if (result == NO)
RMLog(@"Error expiring cache");

[[db executeQuery:@"VACUUM"] close];
}];

[writeQueueLock unlock];

tileCount = [self countTiles];
}
}

// RMLog(@"DB cache hit tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]); // RMLog(@"DB cache hit tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]);


return cachedImage; return cachedImage;
Expand All @@ -217,7 +247,7 @@ - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)
{ {
NSUInteger tilesInDb = [self count]; NSUInteger tilesInDb = [self count];


if (capacity <= tilesInDb) if (capacity <= tilesInDb && expiryPeriod == 0)
[self purgeTiles:MAX(minimalPurge, 1+tilesInDb-capacity)]; [self purgeTiles:MAX(minimalPurge, 1+tilesInDb-capacity)];


// RMLog(@"DB cache insert tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]); // RMLog(@"DB cache insert tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]);
Expand Down
3 changes: 3 additions & 0 deletions MapView/Map/RMTileCache.h
Expand Up @@ -65,8 +65,11 @@ typedef enum {
// This one has its own variable because we want to propagate cache hits down in // This one has its own variable because we want to propagate cache hits down in
// the cache hierarchy up to the memory cache // the cache hierarchy up to the memory cache
RMMemoryCache *memoryCache; RMMemoryCache *memoryCache;
NSTimeInterval expiryPeriod;
} }


- (id)initWithExpiryPeriod:(NSTimeInterval)period;

+ (NSNumber *)tileHash:(RMTile)tile; + (NSNumber *)tileHash:(RMTile)tile;


// Add another cache to the chain // Add another cache to the chain
Expand Down
16 changes: 15 additions & 1 deletion MapView/Map/RMTileCache.m
Expand Up @@ -41,13 +41,14 @@ @interface RMTileCache (Configuration)


@implementation RMTileCache @implementation RMTileCache


- (id)init - (id)initWithExpiryPeriod:(NSTimeInterval)period
{ {
if (!(self = [super init])) if (!(self = [super init]))
return nil; return nil;


caches = [[NSMutableArray alloc] init]; caches = [[NSMutableArray alloc] init];
memoryCache = nil; memoryCache = nil;
expiryPeriod = period;


id cacheCfg = [[RMConfiguration configuration] cacheConfiguration]; id cacheCfg = [[RMConfiguration configuration] cacheConfiguration];
if (!cacheCfg) if (!cacheCfg)
Expand Down Expand Up @@ -87,6 +88,14 @@ - (id)init
return self; return self;
} }


- (id)init
{
if (!(self = [self initWithExpiryPeriod:0]))
return nil;

return self;
}

- (void)dealloc - (void)dealloc
{ {
[memoryCache release]; memoryCache = nil; [memoryCache release]; memoryCache = nil;
Expand Down Expand Up @@ -230,11 +239,16 @@ @implementation RMTileCache (Configuration)
RMLog(@"minimalPurge must be at least one and at most the cache capacity"); RMLog(@"minimalPurge must be at least one and at most the cache capacity");
} }
} }

NSNumber *expiryPeriodNumber = [cfg objectForKey:@"expiryPeriod"];
if (expiryPeriodNumber != nil)
expiryPeriod = [expiryPeriodNumber intValue];


RMDatabaseCache *dbCache = [[[RMDatabaseCache alloc] initUsingCacheDir:useCacheDir] autorelease]; RMDatabaseCache *dbCache = [[[RMDatabaseCache alloc] initUsingCacheDir:useCacheDir] autorelease];
[dbCache setCapacity:capacity]; [dbCache setCapacity:capacity];
[dbCache setPurgeStrategy:strategy]; [dbCache setPurgeStrategy:strategy];
[dbCache setMinimalPurge:minimalPurge]; [dbCache setMinimalPurge:minimalPurge];
[dbCache setExpiryPeriod:expiryPeriod];


return dbCache; return dbCache;
} }
Expand Down

0 comments on commit 037667b

Please sign in to comment.