Improve speed of freshness and storage

This commit is contained in:
Jon Staab
2024-09-18 10:11:00 -07:00
parent 315f53e9b2
commit 2b1cb5fa33
3 changed files with 22 additions and 16 deletions
+12 -5
View File
@@ -1,7 +1,13 @@
import {writable} from 'svelte/store'
import {assoc} from '@welshman/lib'
import {assoc, batch} from '@welshman/lib'
import {withGetter} from '@welshman/store'
export type FreshnessUpdate = {
ns: string
key: string
ts: number
}
export const freshness = withGetter(writable<Record<string, number>>({}))
export const getFreshnessKey = (ns: string, key: string) => `${ns}:${key}`
@@ -9,14 +15,15 @@ export const getFreshnessKey = (ns: string, key: string) => `${ns}:${key}`
export const getFreshness = (ns: string, key: string) =>
freshness.get()[getFreshnessKey(ns, key)] || 0
export const setFreshness = (ns: string, key: string, ts: number) =>
export const setFreshnessImmediate = ({ns, key, ts}: FreshnessUpdate) =>
freshness.update(assoc(getFreshnessKey(ns, key), ts))
export const setFreshnessBulk = (ns: string, updates: Record<string, number>) =>
export const setFreshnessThrottled = batch(100, (updates: FreshnessUpdate[]) =>
freshness.update($freshness => {
for (const [key, ts] of Object.entries(updates)) {
$freshness[key] = ts
for (const {ns, key, ts} of updates) {
$freshness[getFreshnessKey(ns, key)] = ts
}
return $freshness
})
)