Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #61 from mapbox/cache-expiry

configurable cache expiration handling
  • Loading branch information...
commit 55d3ab4c3cdd24341d5caba56b18bddecf366d8f 2 parents babae82 + 037667b
@trasch trasch authored
View
1  MapView/Map/RMDatabaseCache.h
@@ -43,5 +43,6 @@
- (void)setPurgeStrategy:(RMCachePurgeStrategy)theStrategy;
- (void)setCapacity:(NSUInteger)theCapacity;
- (void)setMinimalPurge:(NSUInteger)thePurgeMinimum;
+- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod;
@end
View
32 MapView/Map/RMDatabaseCache.m
@@ -57,6 +57,7 @@ @implementation RMDatabaseCache
RMCachePurgeStrategy purgeStrategy;
NSUInteger capacity;
NSUInteger minimalPurge;
+ NSTimeInterval expiryPeriod;
}
@synthesize databasePath;
@@ -169,6 +170,13 @@ - (void)setMinimalPurge:(NSUInteger)theMinimalPurge
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 @@ - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
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 @@ - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)
{
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]);
View
3  MapView/Map/RMTileCache.h
@@ -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
View
16 MapView/Map/RMTileCache.m
@@ -41,13 +41,14 @@ @interface RMTileCache (Configuration)
@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 @@ - (id)init
return self;
}
+- (id)init
+{
+ if (!(self = [self initWithExpiryPeriod:0]))
+ return nil;
+
+ return self;
+}
+
- (void)dealloc
{
[memoryCache release]; memoryCache = nil;
@@ -230,11 +239,16 @@ @implementation RMTileCache (Configuration)
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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.