From 576fd8ff4021f13fa309011afe74369535379e50 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Sat, 17 Apr 2021 15:42:22 +0200 Subject: [PATCH] Add cache timeout --- Customizations/JSON/LayoutConfig.ts | 5 +++++ Customizations/JSON/LayoutConfigJson.ts | 19 +++++++++++++++++++ Logic/FeatureSource/LocalStorageSaver.ts | 4 ++++ Models/Constants.ts | 2 +- assets/themes/grb.json | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Customizations/JSON/LayoutConfig.ts b/Customizations/JSON/LayoutConfig.ts index 91e4d56..0da4f1e 100644 --- a/Customizations/JSON/LayoutConfig.ts +++ b/Customizations/JSON/LayoutConfig.ts @@ -42,6 +42,10 @@ export default class LayoutConfig { public readonly enableGeolocation: boolean; public readonly enableBackgroundLayerSelection: boolean; public readonly customCss?: string; + /* + How long is the cache valid, in seconds? + */ + public readonly cacheTimeout?: number; private readonly _official: boolean; constructor(json: LayoutConfigJson, official = true, context?: string) { @@ -167,6 +171,7 @@ export default class LayoutConfig { this.enableAddNewPoints = json.enableAddNewPoints ?? true; this.enableBackgroundLayerSelection = json.enableBackgroundLayerSelection ?? true; this.customCss = json.customCss; + this.cacheTimeout = json.cacheTimout ?? (60 * 24 * 60 * 60) } public CustomCodeSnippets(): string[] { diff --git a/Customizations/JSON/LayoutConfigJson.ts b/Customizations/JSON/LayoutConfigJson.ts index 0b63c72..e70677e 100644 --- a/Customizations/JSON/LayoutConfigJson.ts +++ b/Customizations/JSON/LayoutConfigJson.ts @@ -118,6 +118,25 @@ export interface LayoutConfigJson { * The id of the default background. BY default: vanilla OSM */ defaultBackgroundId?: string; + + /** + * 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; /** diff --git a/Logic/FeatureSource/LocalStorageSaver.ts b/Logic/FeatureSource/LocalStorageSaver.ts index 6dc4630..e97780d 100644 --- a/Logic/FeatureSource/LocalStorageSaver.ts +++ b/Logic/FeatureSource/LocalStorageSaver.ts @@ -15,6 +15,10 @@ export default class LocalStorageSaver implements FeatureSource { this.features = source.features; this.features.addCallbackAndRun(features => { + + const now = new Date().getTime() + features = features.filter(f => layout.data.cacheTimeout > Math.abs(now - f.freshness.getTime())/1000) + if (features === undefined) { return; } diff --git a/Models/Constants.ts b/Models/Constants.ts index ababae8..efc8ffe 100644 --- a/Models/Constants.ts +++ b/Models/Constants.ts @@ -2,7 +2,7 @@ import { Utils } from "../Utils"; export default class Constants { - public static vNumber = "0.6.8b"; + public static vNumber = "0.6.8c"; // The user journey states thresholds when a new feature gets unlocked public static userJourney = { diff --git a/assets/themes/grb.json b/assets/themes/grb.json index 1ade2d1..7f38732 100644 --- a/assets/themes/grb.json +++ b/assets/themes/grb.json @@ -19,6 +19,7 @@ "startLon": 3.231, "startZoom": 14, "widenFactor": 0.05, + "cacheTimeout": 3600, "socialImage": "", "layers": [ {