#307 Add ipp.shareCacheTime and cachebusted link after an upload
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -108,3 +108,16 @@ describe('TtlLruCache re-set', () => {
|
||||
expect(cache.get('a')).toBe('2')
|
||||
})
|
||||
})
|
||||
|
||||
describe('TtlLruCache TTL getter', () => {
|
||||
it('reads the TTL on each set, so a later change applies to the next entry', () => {
|
||||
let ttlMs = 1000
|
||||
const cache = new TtlLruCache<string>({ ttlMs: () => ttlMs, max: 10 })
|
||||
cache.set('a', '1')
|
||||
ttlMs = 100
|
||||
cache.set('b', '2')
|
||||
vi.advanceTimersByTime(100)
|
||||
expect(cache.get('a')).toBe('1')
|
||||
expect(cache.get('b')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user