Ignore user provided tile cache size when too small

This commit is contained in:
Andreas Hocevar
2020-02-27 13:39:00 +01:00
parent df1d0ac4a0
commit a072e3acea
16 changed files with 41 additions and 30 deletions

View File

@@ -7,7 +7,7 @@ import {fromKey, getKey} from './tilecoord.js';
class TileCache extends LRUCache { class TileCache extends LRUCache {
/** /**
* @param {!Object<string, import("./TileRange.js").default>} usedTiles Used tiles. * @param {!Object<string, boolean>} usedTiles Used tiles.
*/ */
expireCache(usedTiles) { expireCache(usedTiles) {
while (this.canExpireCache()) { while (this.canExpireCache()) {

View File

@@ -50,7 +50,7 @@ const TOS_ATTRIBUTION = '<a class="ol-attribution-bing-tos" ' +
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {boolean} [hidpi=false] If `true` hidpi tiles will be requested. * @property {boolean} [hidpi=false] If `true` hidpi tiles will be requested.
* @property {string} [culture='en-us'] Culture code. * @property {string} [culture='en-us'] Culture code.
* @property {string} key Bing Maps API key. Get yours at http://www.bingmapsportal.com/. * @property {string} key Bing Maps API key. Get yours at http://www.bingmapsportal.com/.

View File

@@ -9,7 +9,7 @@ import XYZ from './XYZ.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -20,7 +20,7 @@ export const ATTRIBUTION = '&#169; ' +
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin='anonymous'] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin='anonymous'] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -90,7 +90,7 @@ const ProviderConfig = {
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {string} layer Layer name. * @property {string} layer Layer name.
* @property {number} [minZoom] Minimum zoom. * @property {number} [minZoom] Minimum zoom.
* @property {number} [maxZoom] Maximum zoom. * @property {number} [maxZoom] Maximum zoom.

View File

@@ -69,24 +69,21 @@ class TileSource extends Source {
*/ */
this.tileGrid = options.tileGrid !== undefined ? options.tileGrid : null; this.tileGrid = options.tileGrid !== undefined ? options.tileGrid : null;
let cacheSize = options.cacheSize; const tileSize = [256, 256];
if (cacheSize === undefined) { const tileGrid = options.tileGrid;
const tileSize = [256, 256]; if (tileGrid) {
const tileGrid = options.tileGrid; toSize(tileGrid.getTileSize(tileGrid.getMinZoom()), tileSize);
if (tileGrid) {
toSize(tileGrid.getTileSize(tileGrid.getMinZoom()), tileSize);
}
const canUseScreen = typeof screen !== 'undefined';
const width = canUseScreen ? (screen.availWidth || screen.width) : 1920;
const height = canUseScreen ? (screen.availHeight || screen.height) : 1080;
cacheSize = 4 * Math.ceil(width / tileSize[0]) * Math.ceil(height / tileSize[1]);
} }
const canUseScreen = typeof screen !== 'undefined';
const width = canUseScreen ? (screen.availWidth || screen.width) : 1920;
const height = canUseScreen ? (screen.availHeight || screen.height) : 1080;
const minCacheSize = 4 * Math.ceil(width / tileSize[0]) * Math.ceil(height / tileSize[1]);
/** /**
* @protected * @protected
* @type {import("../TileCache.js").default} * @type {import("../TileCache.js").default}
*/ */
this.tileCache = new TileCache(cacheSize); this.tileCache = new TileCache(Math.max(minCacheSize, options.cacheSize || 0));
/** /**
* @protected * @protected
@@ -125,7 +122,7 @@ class TileSource extends Source {
/** /**
* @param {import("../proj/Projection.js").default} projection Projection. * @param {import("../proj/Projection.js").default} projection Projection.
* @param {!Object<string, import("../TileRange.js").default>} usedTiles Used tiles. * @param {!Object<string, boolean>} usedTiles Used tiles.
*/ */
expireCache(projection, usedTiles) { expireCache(projection, usedTiles) {
const tileCache = this.getTileCacheForProjection(projection); const tileCache = this.getTileCacheForProjection(projection);

View File

@@ -13,7 +13,7 @@ import {appendParams} from '../uri.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -17,7 +17,7 @@ import {getForProjection as getTileGridForProjection} from '../tilegrid.js';
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {boolean} [attributionsCollapsible=true] Attributions are collapsible. * @property {boolean} [attributionsCollapsible=true] Attributions are collapsible.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -39,7 +39,7 @@ import {createXYZ, extentFromProjection} from '../tilegrid.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -20,7 +20,7 @@ import {appendParams} from '../uri.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -227,6 +227,15 @@ class VectorTile extends UrlTile {
this.sourceTileCache.clear(); this.sourceTileCache.clear();
} }
/**
* @param {import("../proj/Projection.js").default} projection Projection.
* @param {!Object<string, boolean>} usedTiles Used tiles.
*/
expireCache(projection, usedTiles) {
super.expireCache(projection, usedTiles);
this.sourceTileCache.expireCache({});
}
/** /**
* @param {number} pixelRatio Pixel ratio. * @param {number} pixelRatio Pixel ratio.
* @param {import("../proj/Projection").default} projection Projection. * @param {import("../proj/Projection").default} projection Projection.
@@ -328,7 +337,6 @@ class VectorTile extends UrlTile {
tile.sourceTiles = sourceTiles; tile.sourceTiles = sourceTiles;
} }
} }
this.sourceTileCache.expireCache({});
return sourceTiles; return sourceTiles;
} }

View File

@@ -15,7 +15,7 @@ import {appendParams} from '../uri.js';
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -9,7 +9,7 @@ import {createXYZ, extentFromProjection} from '../tilegrid.js';
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {boolean} [attributionsCollapsible=true] Attributions are collapsible. * @property {boolean} [attributionsCollapsible=true] Attributions are collapsible.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -80,7 +80,7 @@ export class CustomTile extends ImageTile {
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {import("./Source.js").AttributionLike} [attributions] Attributions. * @property {import("./Source.js").AttributionLike} [attributions] Attributions.
* @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will increase if too small. * @property {number} [cacheSize] Tile cache size. The default depends on the screen size. Will be ignored if too small.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that * @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value you want to access pixel data with the Canvas renderer. * you must provide a `crossOrigin` value you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail. * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.

View File

@@ -63,13 +63,19 @@ describe('ol.source.Tile', function() {
const source = new TileSource({}); const source = new TileSource({});
expect(source.tileCache.highWaterMark).to.be(4 * Math.ceil(screen.availWidth / 256) * Math.ceil(screen.availHeight / 256)); expect(source.tileCache.highWaterMark).to.be(4 * Math.ceil(screen.availWidth / 256) * Math.ceil(screen.availHeight / 256));
}); });
it('ignores a cache size that is too small', function() {
const source = new TileSource({
cacheSize: 1
});
expect(source.tileCache.highWaterMark).to.be(4 * Math.ceil(screen.availWidth / 256) * Math.ceil(screen.availHeight / 256));
});
it('sets a custom cache size', function() { it('sets a custom cache size', function() {
const projection = getProjection('EPSG:4326'); const projection = getProjection('EPSG:4326');
const source = new TileSource({ const source = new TileSource({
projection: projection, projection: projection,
cacheSize: 42 cacheSize: 442
}); });
expect(source.getTileCacheForProjection(projection).highWaterMark).to.be(42); expect(source.getTileCacheForProjection(projection).highWaterMark).to.be(442);
}); });
}); });

View File

@@ -26,9 +26,9 @@ describe('ol.source.TileImage', function() {
describe('#getTileCacheForProjection', function() { describe('#getTileCacheForProjection', function() {
it('uses the cacheSize for reprojected tile caches', function() { it('uses the cacheSize for reprojected tile caches', function() {
const source = createSource(undefined, undefined, 42); const source = createSource(undefined, undefined, 442);
const tileCache = source.getTileCacheForProjection(getProjection('EPSG:4326')); const tileCache = source.getTileCacheForProjection(getProjection('EPSG:4326'));
expect(tileCache.highWaterMark).to.be(42); expect(tileCache.highWaterMark).to.be(442);
expect(tileCache).to.not.equal(source.getTileCacheForProjection(source.getProjection())); expect(tileCache).to.not.equal(source.getTileCacheForProjection(source.getProjection()));
}); });
}); });