From 3dffcde3b515472243eac3b165809813444d20c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Wed, 10 May 2017 14:22:07 +0200 Subject: [PATCH] :zap: only keep 16 tiles in memory --- src/TileSource.coffee | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/TileSource.coffee b/src/TileSource.coffee index b30485f..dd0e100 100644 --- a/src/TileSource.coffee +++ b/src/TileSource.coffee @@ -25,6 +25,9 @@ catch module.exports = class TileSource cache: {} + cacheSize: 16 + cached: [] + modes: MBTiles: 1 VectorTile: 2 @@ -67,6 +70,10 @@ module.exports = class TileSource if cached = @cache[[z,x,y].join("-")] return Promise.resolve cached + if @cached.length > @cacheSize + for tile in @cached.splice 0, Math.abs(@cacheSize-@cached.length) + delete @cache[tile] + switch @mode when @modes.MBTiles then @_getMBTile z, x, y when @modes.HTTP then @_getHTTP z, x, y @@ -93,7 +100,10 @@ module.exports = class TileSource resolve @_createTile z, x, y, buffer _createTile: (z, x, y, buffer) -> - tile = @cache[[z,x,y].join("-")] = new Tile @styler + name = [z,x,y].join("-") + @cached.push name + + tile = @cache[name] = new Tile @styler tile.load buffer _initPersistence: ->