Add cache timeout option on layerSource

This commit is contained in:
Pieter Vander Vennet 2021-10-25 20:38:57 +02:00
parent 1bc7978f7c
commit c99e15eed9
7 changed files with 67 additions and 51 deletions

View file

@ -68,7 +68,7 @@ export default class FeaturePipeline {
private readonly freshnesses = new Map<string, TileFreshnessCalculator>();
private readonly oldestAllowedDate: Date = new Date(new Date().getTime() - 60 * 60 * 24 * 30 * 1000);
private readonly oldestAllowedDate: Date;
private readonly osmSourceZoomLevel
constructor(
@ -90,6 +90,11 @@ export default class FeaturePipeline {
this.state = state;
const self = this
const expiryInSeconds = Math.min(...state.layoutToUse.layers.map(l => l.maxAgeOfCache))
this.oldestAllowedDate = new Date(new Date().getTime() - expiryInSeconds);
for (const layer of state.layoutToUse.layers) {
TiledFromLocalStorageSource.cleanCacheForLayer(layer)
}
this.osmSourceZoomLevel = state.osmApiTileSize.data;
// milliseconds
const useOsmApi = state.locationControl.map(l => l.zoom > (state.overpassMaxZoom.data ?? 12))

View file

@ -5,6 +5,7 @@ import TileHierarchy from "./TileHierarchy";
import SaveTileToLocalStorageActor from "../Actors/SaveTileToLocalStorageActor";
import {Tiles} from "../../../Models/TileRange";
import {BBox} from "../../BBox";
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig";
export default class TiledFromLocalStorageSource implements TileHierarchy<FeatureSourceForLayer & Tiled> {
public readonly loadedTiles: Map<number, FeatureSourceForLayer & Tiled> = new Map<number, FeatureSourceForLayer & Tiled>();
@ -28,6 +29,29 @@ export default class TiledFromLocalStorageSource implements TileHierarchy<Featur
return freshnesses
}
static cleanCacheForLayer(layer: LayerConfig) {
const now = new Date()
const prefix = SaveTileToLocalStorageActor.storageKey + "-" + layer.id + "-"
console.log("Cleaning tiles of ", prefix, "with max age",layer.maxAgeOfCache)
for (const key of Object.keys(localStorage)) {
if (!(key.startsWith(prefix) && key.endsWith("-time"))) {
continue
}
const index = Number(key.substring(prefix.length, key.length - "-time".length))
const time = Number(localStorage.getItem(key))
const timeDiff = (now.getTime() - time) / 1000
if(timeDiff >= layer.maxAgeOfCache){
const k = prefix+index;
localStorage.removeItem(k)
localStorage.removeItem(k+"-format")
localStorage.removeItem(k+"-time")
console.debug("Removed "+k+" from local storage: too old")
}
}
}
constructor(layer: FilteredLayer,
handleFeatureSource: (src: FeatureSourceForLayer & Tiled, index: number) => void,
state: {

View file

@ -60,8 +60,13 @@ export interface LayerConfigJson {
* NOTE: the previous format was 'overpassTags: AndOrTagConfigJson | string', which is interpreted as a shorthand for source: {osmTags: "key=value"}
* While still supported, this is considered deprecated
*/
source: { osmTags: AndOrTagConfigJson | string, overpassScript?: string } |
{ osmTags: AndOrTagConfigJson | string, geoJson: string, geoJsonZoomLevel?: number, isOsmCache?: boolean }
source: ({ osmTags: AndOrTagConfigJson | string, overpassScript?: string } |
{ osmTags: AndOrTagConfigJson | string, geoJson: string, geoJsonZoomLevel?: number, isOsmCache?: boolean }) & ({
/**
* The maximum amount of seconds that a tile is allowed to linger in the cache
*/
maxCacheAge?: number
})
/**
*

View file

@ -174,26 +174,6 @@ export interface LayoutConfigJson {
*/
tileLayerSources?: TilesourceConfigJson[]
/**
* The number of seconds that a feature is allowed to stay in the cache.
* The caching flow is as following:
*
* 1. The application is opened the first time
* 2. An overpass query is run
* 3. The result is saved to local storage
*
* On the next opening:
*
* 1. The application is opened
* 2. Data is loaded from cache and displayed
* 3. An overpass query is run
* 4. All data (both from overpass ánd local storage) are saved again to local storage (except when to old)
*
* Default value: 60 days
*/
cacheTimout?: number;
/**
* The layers to display.
*

View file

@ -52,6 +52,10 @@ export default class LayerConfig {
public readonly deletion: DeleteConfig | null;
public readonly allowMove: MoveConfig | null
public readonly allowSplit: boolean
/**
* In seconds
*/
public readonly maxAgeOfCache: number
presets: PresetConfig[];
@ -87,7 +91,9 @@ export default class LayerConfig {
// @ts-ignore
legacy = TagUtils.Tag(json["overpassTags"], context + ".overpasstags");
}
if (json.source !== undefined) {
this.maxAgeOfCache = json.source.maxCacheAge ?? 24 * 60 * 60 * 30
if (legacy !== undefined) {
throw (
context +

View file

@ -49,10 +49,7 @@ export default class LayoutConfig {
public readonly enableIframePopout: boolean;
public readonly customCss?: string;
/*
How long is the cache valid, in seconds?
*/
public readonly cacheTimeout?: number;
public readonly overpassUrl: string[];
public readonly overpassTimeout: number;
public readonly overpassMaxZoom: number
@ -170,7 +167,6 @@ export default class LayoutConfig {
this.enablePdfDownload = json.enablePdfDownload ?? false;
this.enableIframePopout = json.enableIframePopout ?? true
this.customCss = json.customCss;
this.cacheTimeout = json.cacheTimout ?? (60 * 24 * 60 * 60)
this.overpassUrl = Constants.defaultOverpassUrls
if(json.overpassUrl !== undefined){
if(typeof json.overpassUrl === "string"){

View file

@ -19,7 +19,6 @@
"startLon": 3.231,
"startZoom": 14,
"widenFactor": 2,
"cacheTimeout": 3600,
"socialImage": "",
"layers": [
{
@ -29,6 +28,7 @@
},
"minzoom": 12,
"source": {
"maxCacheAge": 0,
"osmTags": {
"and": [
"fixme~*",