Merge pull request #151 from twpayne/tile-cache-expiry
Tile cache expiry
This commit is contained in:
@@ -219,6 +219,7 @@ ol.renderer.canvas.TileLayer.prototype.renderFrame =
|
||||
}
|
||||
|
||||
this.updateUsedTiles(frameState.usedTiles, tileSource, z, tileRange);
|
||||
this.scheduleExpireCache(frameState, tileSource);
|
||||
|
||||
var transform = this.transform_;
|
||||
goog.vec.Mat4.makeIdentity(transform);
|
||||
|
||||
@@ -239,6 +239,7 @@ ol.renderer.dom.TileLayer.prototype.renderFrame =
|
||||
}
|
||||
|
||||
this.updateUsedTiles(frameState.usedTiles, tileSource, z, tileRange);
|
||||
this.scheduleExpireCache(frameState, tileSource);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@ goog.provide('ol.renderer.Layer');
|
||||
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.FrameState');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.TileRange');
|
||||
goog.require('ol.layer.Layer');
|
||||
goog.require('ol.layer.LayerProperty');
|
||||
goog.require('ol.source.TileSource');
|
||||
|
||||
|
||||
|
||||
@@ -128,6 +130,23 @@ ol.renderer.Layer.prototype.handleLayerSaturationChange = goog.nullFunction;
|
||||
ol.renderer.Layer.prototype.handleLayerVisibleChange = goog.nullFunction;
|
||||
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @param {ol.FrameState} frameState Frame state.
|
||||
* @param {ol.source.TileSource} tileSource Tile source.
|
||||
*/
|
||||
ol.renderer.Layer.prototype.scheduleExpireCache =
|
||||
function(frameState, tileSource) {
|
||||
if (tileSource.canExpireCache()) {
|
||||
frameState.postRenderFunctions.push(
|
||||
goog.partial(function(tileSource, map, frameState) {
|
||||
var tileSourceKey = goog.getUid(tileSource).toString();
|
||||
tileSource.expireCache(frameState.usedTiles[tileSourceKey]);
|
||||
}, tileSource));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @param {Object.<string, Object.<string, ol.TileRange>>} usedTiles Used tiles.
|
||||
|
||||
@@ -462,6 +462,7 @@ ol.renderer.webgl.TileLayer.prototype.renderFrame =
|
||||
}
|
||||
|
||||
this.updateUsedTiles(frameState.usedTiles, tileSource, z, tileRange);
|
||||
this.scheduleExpireCache(frameState, tileSource);
|
||||
|
||||
goog.vec.Mat4.makeIdentity(this.matrix_);
|
||||
goog.vec.Mat4.translate(this.matrix_,
|
||||
|
||||
@@ -2,6 +2,7 @@ goog.provide('ol.source.DebugTileSource');
|
||||
|
||||
goog.require('ol.Size');
|
||||
goog.require('ol.Tile');
|
||||
goog.require('ol.TileCache');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.source.TileSource');
|
||||
goog.require('ol.tilegrid.TileGrid');
|
||||
@@ -94,26 +95,40 @@ ol.source.DebugTileSource = function(options) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<string, ol.DebugTile_>}
|
||||
* FIXME will need to expire elements from this cache
|
||||
* FIXME see elemoine's work with goog.structs.LinkedMap
|
||||
* @type {ol.TileCache}
|
||||
*/
|
||||
this.tileCache_ = {};
|
||||
this.tileCache_ = new ol.TileCache();
|
||||
|
||||
};
|
||||
goog.inherits(ol.source.DebugTileSource, ol.source.TileSource);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.DebugTileSource.prototype.canExpireCache = function() {
|
||||
return this.tileCache_.canExpireCache();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.DebugTileSource.prototype.expireCache = function(usedTiles) {
|
||||
this.tileCache_.expireCache(usedTiles);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.DebugTileSource.prototype.getTile = function(tileCoord) {
|
||||
var key = tileCoord.toString();
|
||||
if (goog.object.containsKey(this.tileCache_, key)) {
|
||||
return this.tileCache_[key];
|
||||
if (this.tileCache_.containsKey(key)) {
|
||||
return /** @type {ol.DebugTile_} */ (this.tileCache_.get(key));
|
||||
} else {
|
||||
var tile = new ol.DebugTile_(tileCoord, this.tileGrid);
|
||||
this.tileCache_[key] = tile;
|
||||
this.tileCache_.set(key, tile);
|
||||
return tile;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ goog.require('ol.Attribution');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.ImageTile');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.TileCache');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileUrlFunction');
|
||||
goog.require('ol.TileUrlFunctionType');
|
||||
@@ -55,32 +56,46 @@ ol.source.ImageTileSource = function(options) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<string, ol.ImageTile>}
|
||||
* FIXME will need to expire elements from this cache
|
||||
* FIXME see elemoine's work with goog.structs.LinkedMap
|
||||
* @type {ol.TileCache}
|
||||
*/
|
||||
this.tileCache_ = {};
|
||||
this.tileCache_ = new ol.TileCache();
|
||||
|
||||
};
|
||||
goog.inherits(ol.source.ImageTileSource, ol.source.TileSource);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.ImageTileSource.prototype.canExpireCache = function() {
|
||||
return this.tileCache_.canExpireCache();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.ImageTileSource.prototype.expireCache = function(usedTiles) {
|
||||
this.tileCache_.expireCache(usedTiles);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.source.ImageTileSource.prototype.getTile = function(tileCoord) {
|
||||
var key = tileCoord.toString();
|
||||
if (goog.object.containsKey(this.tileCache_, key)) {
|
||||
return this.tileCache_[key];
|
||||
if (this.tileCache_.containsKey(key)) {
|
||||
return /** @type {ol.Tile} */ (this.tileCache_.get(key));
|
||||
} else {
|
||||
var tileUrl = this.getTileCoordUrl(tileCoord);
|
||||
var tile;
|
||||
if (goog.isDef(tileUrl)) {
|
||||
tile = new ol.ImageTile(tileCoord, tileUrl, this.crossOrigin_);
|
||||
this.tileCache_.set(key, tile);
|
||||
} else {
|
||||
tile = null;
|
||||
}
|
||||
this.tileCache_[key] = tile;
|
||||
return tile;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
goog.provide('ol.source.TileSource');
|
||||
goog.provide('ol.source.TileSourceOptions');
|
||||
|
||||
goog.require('goog.functions');
|
||||
goog.require('ol.Attribution');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.Projection');
|
||||
@@ -46,6 +47,18 @@ ol.source.TileSource = function(tileSourceOptions) {
|
||||
goog.inherits(ol.source.TileSource, ol.source.Source);
|
||||
|
||||
|
||||
/**
|
||||
* @return {boolean} Can expire cache.
|
||||
*/
|
||||
ol.source.TileSource.prototype.canExpireCache = goog.functions.FALSE;
|
||||
|
||||
|
||||
/**
|
||||
* @param {Object.<string, ol.TileRange>} usedTiles Used tiles.
|
||||
*/
|
||||
ol.source.TileSource.prototype.expireCache = goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
58
src/ol/tilecache.js
Normal file
58
src/ol/tilecache.js
Normal file
@@ -0,0 +1,58 @@
|
||||
goog.provide('ol.TileCache');
|
||||
|
||||
goog.require('goog.dispose');
|
||||
goog.require('goog.structs.LinkedMap');
|
||||
goog.require('ol.Tile');
|
||||
goog.require('ol.TileRange');
|
||||
|
||||
|
||||
/**
|
||||
* @define {number} Default high water mark.
|
||||
*/
|
||||
ol.DEFAULT_TILE_CACHE_HIGH_WATER_MARK = 512;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {goog.structs.LinkedMap}
|
||||
* @param {number=} opt_highWaterMark High water mark.
|
||||
*/
|
||||
ol.TileCache = function(opt_highWaterMark) {
|
||||
|
||||
goog.base(this, undefined, true);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.highWaterMark_ = goog.isDef(opt_highWaterMark) ?
|
||||
opt_highWaterMark : ol.DEFAULT_TILE_CACHE_HIGH_WATER_MARK;
|
||||
|
||||
};
|
||||
goog.inherits(ol.TileCache, goog.structs.LinkedMap);
|
||||
|
||||
|
||||
/**
|
||||
* @return {boolean} Can expire cache.
|
||||
*/
|
||||
ol.TileCache.prototype.canExpireCache = function() {
|
||||
return this.getCount() > this.highWaterMark_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Object.<string, ol.TileRange>} usedTiles Used tiles.
|
||||
*/
|
||||
ol.TileCache.prototype.expireCache = function(usedTiles) {
|
||||
var tile, zKey;
|
||||
while (this.canExpireCache()) {
|
||||
tile = /** @type {ol.Tile} */ (this.peekLast());
|
||||
zKey = tile.tileCoord.z.toString();
|
||||
if (zKey in usedTiles && usedTiles[zKey].contains(tile.tileCoord)) {
|
||||
break;
|
||||
} else {
|
||||
this.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user