Timed cache using a Storage interface.

Example

Letting cache expire.

const cache = new StorageCache(localStorage, 'my-cache', 30 * 60_000)

cache.set(['a', 'path'], 'foo')
cache.get(['a', 'path'])
// 'foo'

await setTimeout(30 * 60_000)
cache.get(['a', 'path'])
// undefined

Example

Manually invalidating cache.

const cache = new StorageCache(localStorage, 'my-cache', 30 * 60_000)
cache.set(['foo', 'bar'], 'a')
cache.set(['foo', 'rab'], 'b')

// Remove a specific cache item
cache.unset(['foo', 'bar'])

// Remove all "foos"
cache.unset(['foo'])

Constructors

Properties

#key: string
#ms: number
#storage: Storage

Accessors

Methods

  • Checks if a path has cache. If not, updates it and returns true. Otherwise returns false.

    Parameters

    • path: string[]
    • update: (() => unknown)
        • (): unknown
        • Returns unknown

    • Optional ms: number

    Returns Promise<boolean>

Generated using TypeDoc