From 97745c50af836ca1149aea37b584806d276bc02f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 7 Oct 2017 10:29:40 -0600 Subject: [PATCH] Prune all except for the most recent z on URL change --- src/ol/source/urltile.js | 1 + src/ol/tilecache.js | 20 +++++++++++++++++ test/spec/ol/tilecache.test.js | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 test/spec/ol/tilecache.test.js diff --git a/src/ol/source/urltile.js b/src/ol/source/urltile.js index 39c3ffe06fa..8f20a0795ed 100644 --- a/src/ol/source/urltile.js +++ b/src/ol/source/urltile.js @@ -155,6 +155,7 @@ ol.source.UrlTile.prototype.setTileLoadFunction = function(tileLoadFunction) { */ ol.source.UrlTile.prototype.setTileUrlFunction = function(tileUrlFunction, opt_key) { this.tileUrlFunction = tileUrlFunction; + this.tileCache.pruneExceptNewestZ(); if (typeof opt_key !== 'undefined') { this.setKey(opt_key); } else { diff --git a/src/ol/tilecache.js b/src/ol/tilecache.js index 1ba7bbada94..088d9b1e72b 100644 --- a/src/ol/tilecache.js +++ b/src/ol/tilecache.js @@ -2,6 +2,7 @@ goog.provide('ol.TileCache'); goog.require('ol'); goog.require('ol.structs.LRUCache'); +goog.require('ol.tilecoord'); /** @@ -33,3 +34,22 @@ ol.TileCache.prototype.expireCache = function(usedTiles) { } } }; + + +/** + * Prune all tiles from the cache that don't have the same z as the newest tile. + */ +ol.TileCache.prototype.pruneExceptNewestZ = function() { + if (this.getCount() === 0) { + return; + } + var key = this.peekFirstKey(); + var tileCoord = ol.tilecoord.fromKey(key); + var z = tileCoord[0]; + this.forEach(function(tile) { + if (tile.tileCoord[0] !== z) { + this.remove(ol.tilecoord.getKey(tile.tileCoord)); + tile.dispose(); + } + }, this); +}; diff --git a/test/spec/ol/tilecache.test.js b/test/spec/ol/tilecache.test.js new file mode 100644 index 00000000000..d0ab6b7c54f --- /dev/null +++ b/test/spec/ol/tilecache.test.js @@ -0,0 +1,39 @@ +goog.require('ol.Tile'); +goog.require('ol.TileCache'); +goog.require('ol.tilecoord'); + + +describe('ol.TileCache', function() { + + describe('#pruneExceptNewestZ()', function() { + it('gets rid of all entries that are not at the newest z', function() { + var tiles = [ + new ol.Tile([0, 0, 0]), + new ol.Tile([1, 0, 0]), + new ol.Tile([1, 1, 0]), + new ol.Tile([2, 0, 0]), + new ol.Tile([2, 1, 0]), + new ol.Tile([2, 2, 0]), + new ol.Tile([2, 3, 0]) // newest tile at z: 2 + ]; + var cache = new ol.TileCache(); + + sinon.spy(tiles[0], 'dispose'); + + tiles.forEach(function(tile) { + cache.set(ol.tilecoord.getKey(tile.tileCoord), tile); + }); + + cache.pruneExceptNewestZ(); + + expect(cache.getKeys()).to.eql([ + '2/3/0', + '2/2/0', + '2/1/0', + '2/0/0' + ]); + + expect(tiles[0].dispose.calledOnce).to.be(true); + }); + }); +});