Cache base layers but don't clear on bbox clear

This commit is contained in:
Maciej Ziarkowski 2019-10-07 17:27:22 +01:00
parent ed999e1311
commit 27ba5310bb
3 changed files with 28 additions and 6 deletions

View File

@ -111,6 +111,14 @@ const BUILDING_LAYER_DEFINITIONS = {
const GEOMETRY_FIELD = 'geometry_geom';
function getBuildingLayerNames() {
return Object.keys(BUILDING_LAYER_DEFINITIONS);
}
function getAllLayerNames() {
return ['highlight', ...getBuildingLayerNames()];
}
function getBuildingsDataConfig(tileset: string, dataParams: any): DataConfig {
const table = BUILDING_LAYER_DEFINITIONS[tileset];
@ -149,7 +157,8 @@ function getHighlightDataConfig(tileset: string, dataParams: any): DataConfig {
}
export {
BUILDING_LAYER_DEFINITIONS,
getBuildingLayerNames,
getAllLayerNames,
getBuildingsDataConfig,
getHighlightDataConfig
};

View File

@ -1,6 +1,6 @@
import { TileCache } from "./tileCache";
import { BoundingBox, TileParams, Tile } from "./types";
import { getBuildingsDataConfig, getHighlightDataConfig, BUILDING_LAYER_DEFINITIONS } from "./dataDefinition";
import { getBuildingsDataConfig, getHighlightDataConfig, getAllLayerNames, getBuildingLayerNames } from "./dataDefinition";
import { isOutsideExtent } from "./util";
import { renderDataSourceTile } from "./renderers/renderDataSourceTile";
import { getTileWithCaching } from "./renderers/getTileWithCaching";
@ -10,7 +10,7 @@ import { createBlankTile } from "./renderers/createBlankTile";
/**
* A list of all tilesets handled by the tile server
*/
const allTilesets = ['highlight', ...Object.keys(BUILDING_LAYER_DEFINITIONS)];
const allTilesets = getAllLayerNames();
/**
* Zoom level when we switch from rendering direct from database to instead composing tiles
@ -27,14 +27,19 @@ const EXTENT_BBOX: BoundingBox = [-61149.622628, 6667754.851372, 28128.826409, 6
const tileCache = new TileCache(
process.env.TILECACHE_PATH,
{
tilesets: ['date_year', 'size_storeys', 'location', 'likes', 'conservation_area', 'sust_dec', 'building_attachment_form'],
tilesets: getBuildingLayerNames(),
minZoom: 9,
maxZoom: 18,
scales: [1, 2]
},
// cache age data and base building outlines for more zoom levels than other layers
({ tileset, z }: TileParams) => (tileset === 'date_year' && z <= 16) ||
((tileset === 'base_light' || tileset === 'base_night') && z <= 17) ||
z <= 13
z <= 13,
// don't clear base_light and base_night on bounding box cache clear
(tileset: string) => tileset !== 'base_light' && tileset !== 'base_night'
);
const renderBuildingTile = (t: TileParams, d: any) => renderDataSourceTile(t, d, getBuildingsDataConfig);

View File

@ -70,7 +70,9 @@ class TileCache {
/** Domain definition for the cache */
private cacheDomain: CacheDomain,
/** Function for defining custom caching rules (optional) */
private shouldCacheFn?: (TileParams) => boolean
private shouldCacheFn?: (TileParams) => boolean,
/** Function for defining whether the tileset should be cleared when clearing cache for bounding box */
private shouldBulkClearTilesetFn?: (tileset: string) => boolean
) {}
async get(tileParams: TileParams): Promise<Image> {
@ -108,6 +110,8 @@ class TileCache {
async removeAllAtBbox(bbox: BoundingBox): Promise<void[]> {
const removePromises: Promise<void>[] = [];
for (const tileset of this.cacheDomain.tilesets) {
if(!this.shouldBulkClearTileset(tileset)) continue;
for (let z = this.cacheDomain.minZoom; z <= this.cacheDomain.maxZoom; z++) {
let tileBounds = getXYZ(bbox, z)
for (let x = tileBounds.minX; x <= tileBounds.maxX; x++) {
@ -137,6 +141,10 @@ class TileCache {
this.cacheDomain.scales.includes(tileParams.scale) &&
(this.shouldCacheFn == undefined || this.shouldCacheFn(tileParams));
}
private shouldBulkClearTileset(tileset: string): boolean {
return this.shouldCacheFn == undefined || this.shouldBulkClearTilesetFn(tileset);
}
}
export {