#307 Add ipp.shareCacheTime and cachebusted link after an upload

This commit is contained in:
Alan Grainger
2026-10-19 14:42:43 +13:00
parent 8bf1d753aa
commit a958559247
11 changed files with 159 additions and 11 deletions
+8 -3
View File
@@ -22,10 +22,14 @@ interface Entry<V> {
export class TtlLruCache<V> {
private readonly entries = new Map<string, Entry<V>>()
private readonly ttlMs: number
private readonly ttlMs: number | (() => number)
private readonly max: number
constructor (opts: { ttlMs: number, max: number }) {
/**
* `ttlMs` may be a function, read on each `set`, for a TTL that comes from
* config: a cache built at import time exists before the config is loaded.
*/
constructor (opts: { ttlMs: number | (() => number), max: number }) {
this.ttlMs = opts.ttlMs
this.max = opts.max
}
@@ -55,7 +59,8 @@ export class TtlLruCache<V> {
* just in case).
*/
set (key: string, value: V): void {
this.entries.set(key, { value, expiresAt: Date.now() + this.ttlMs })
const ttlMs = typeof this.ttlMs === 'function' ? this.ttlMs() : this.ttlMs
this.entries.set(key, { value, expiresAt: Date.now() + ttlMs })
if (this.entries.size > this.max) {
const oldest = this.entries.keys().next().value
if (oldest !== undefined && oldest !== key) this.entries.delete(oldest)