diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-13 14:48:11 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-13 14:48:21 +0100 |
| commit | a8219611cbebbd27501d9f30c804979048b98107 (patch) | |
| tree | 632dbab8b724f0148b06525771d85ad6ddb55e35 /code/app/src/help/cache.ts | |
| parent | db2d937a38d5c309e3c6aa14f2eaf3cfe58f415e (diff) | |
| download | greatoffice-a8219611cbebbd27501d9f30c804979048b98107.tar.xz greatoffice-a8219611cbebbd27501d9f30c804979048b98107.zip | |
feat: A whole slew of things
- Use a md5 hash of the session cookie value as key for session validity check
- Introduce global state
- Introduce a common interface for form logic, and implement it on the sign-in form
- Introduce static resolve() on all services instead of new-upping all over.
- Implement /portal on the frontend to support giving the frontend a inital context from server or anywhere.
- Show a notification when users sign in for the first time after validating their email
Diffstat (limited to 'code/app/src/help/cache.ts')
| -rw-r--r-- | code/app/src/help/cache.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/code/app/src/help/cache.ts b/code/app/src/help/cache.ts new file mode 100644 index 0000000..e253399 --- /dev/null +++ b/code/app/src/help/cache.ts @@ -0,0 +1,38 @@ +import { Temporal } from "temporal-polyfill"; +import { log_debug } from "$help/logger"; + +let cache = {}; + +export const CacheKeys = { + isAuthenticated: "isAuthenticated" +} + +export async function cached_result_async<T>(key: string, staleAfterSeconds: number, get_result: any, forceRefresh: boolean = false) { + if (!cache[key]) { + cache[key] = { + l: 0, + c: undefined as T, + }; + } + const staleEpoch = ((cache[key]?.l ?? 0) + staleAfterSeconds); + const isStale = forceRefresh || (staleEpoch < Temporal.Now.instant().epochSeconds); + if (isStale || !cache[key]?.c) { + cache[key].c = await get_result(); + cache[key].l = Temporal.Now.instant().epochSeconds; + } + + log_debug("Ran cached_result_async", { + cacheKey: key, + isStale, + cache: cache[key], + staleEpoch, + }); + + return cache[key].c as T; +} + +export function clear_cache(key: string) { + if (!key) throw new Error("No key was specified"); + cache[key].c = undefined; + log_debug("Cleared cache with key: " + key); +}
\ No newline at end of file |
