Authored by Justin R. Miller

Merge branch 'cache-expiry' into develop

... ... @@ -43,5 +43,6 @@
- (void)setPurgeStrategy:(RMCachePurgeStrategy)theStrategy;
- (void)setCapacity:(NSUInteger)theCapacity;
- (void)setMinimalPurge:(NSUInteger)thePurgeMinimum;
- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod;
@end
... ...
... ... @@ -57,6 +57,7 @@
RMCachePurgeStrategy purgeStrategy;
NSUInteger capacity;
NSUInteger minimalPurge;
NSTimeInterval expiryPeriod;
}
@synthesize databasePath;
... ... @@ -169,6 +170,13 @@
minimalPurge = theMinimalPurge;
}
- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod
{
expiryPeriod = theExpiryPeriod;
srand(time(NULL));
}
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
// RMLog(@"DB cache check for tile %d %d %d", tile.x, tile.y, tile.zoom);
... ... @@ -203,6 +211,28 @@
if (capacity != 0 && purgeStrategy == RMCachePurgeStrategyLRU)
[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]);
return cachedImage;
... ... @@ -217,7 +247,7 @@
{
NSUInteger tilesInDb = [self count];
if (capacity <= tilesInDb)
if (capacity <= tilesInDb && expiryPeriod == 0)
[self purgeTiles:MAX(minimalPurge, 1+tilesInDb-capacity)];
// RMLog(@"DB cache insert tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]);
... ...
... ... @@ -65,8 +65,11 @@ typedef enum {
// This one has its own variable because we want to propagate cache hits down in
// the cache hierarchy up to the memory cache
RMMemoryCache *memoryCache;
NSTimeInterval expiryPeriod;
}
- (id)initWithExpiryPeriod:(NSTimeInterval)period;
+ (NSNumber *)tileHash:(RMTile)tile;
// Add another cache to the chain
... ...
... ... @@ -41,13 +41,14 @@
@implementation RMTileCache
- (id)init
- (id)initWithExpiryPeriod:(NSTimeInterval)period
{
if (!(self = [super init]))
return nil;
caches = [[NSMutableArray alloc] init];
memoryCache = nil;
expiryPeriod = period;
id cacheCfg = [[RMConfiguration configuration] cacheConfiguration];
if (!cacheCfg)
... ... @@ -87,6 +88,14 @@
return self;
}
- (id)init
{
if (!(self = [self initWithExpiryPeriod:0]))
return nil;
return self;
}
- (void)dealloc
{
[memoryCache release]; memoryCache = nil;
... ... @@ -230,11 +239,16 @@
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];
[dbCache setCapacity:capacity];
[dbCache setPurgeStrategy:strategy];
[dbCache setMinimalPurge:minimalPurge];
[dbCache setExpiryPeriod:expiryPeriod];
return dbCache;
}
... ...