Infohub JS API
Loading...
Searching...
No Matches
CacheStore Class Reference

Description

@fileoverview Minimal in-memory cache with TTL and tagging.

Small utility used by services to cache HTTP results in case of success. A generic, reusable in-memory cache with optional TTL, metadata, and predicate-based invalidation. Works for any data shape.

Exposed globally as window.CacheStore

Generic in-memory cache with TTL and tag-based invalidation.

Typical usage:

const cache = new CacheStore(5_000); // default TTL 5s
await cache.getOrCompute("key", async () => fetchData());
@fileoverview Minimal in-memory cache with TTL and tagging.
Definition cache.internal.js:26

Public Member Functions

 clear ()
 constructor (defaultTTLms=null)
 delete (key)
 deleteWhere (predicate)
 get (key)
 getEntry (key)
async getOrCompute (key, compute, options={})
 has (key)
 markNeedUpdate (key)
 markNeedUpdateWhere (predicate)
 set (key, value, options={})

Function Documentation

◆ clear()

clear ( )

Clear all entries.

◆ constructor()

constructor ( defaultTTLms = null)
Parameters
{number|null}defaultTTLms default TTL in milliseconds for entries set without an explicit ttl

◆ delete()

delete ( key)

Delete a key.

Parameters
{string}key

◆ deleteWhere()

deleteWhere ( predicate)

Delete entries where the predicate matches.

Parameters
{function(string,{value: *, needUpdate: boolean, tags: (Object<string, *>|undefined) }): boolean} predicate

◆ get()

get ( key)

Get only the value for a key (undefined if missing or expired).

Parameters
{string}key
Returns
{any}

◆ getEntry()

getEntry ( key)

Get the full entry including metadata (undefined if missing or expired).

Parameters
{string}key
Returns
{{ value: *, needUpdate: boolean, tags: (Object<string, *>|undefined) }|undefined}

◆ getOrCompute()

async getOrCompute ( key,
compute,
options = {} )

Get or compute and store a value under a key.

@template T

Parameters
{string}key
{function()(T|Promise<T>)} compute
{Object}[options]
{(number|null)}[options.ttlMs]
{Object<string,*>}[options.tags]
Returns
{Promise<T>} resolves to the value

◆ has()

has ( key)

Returns whether a key exists and is not expired.

Parameters
{string}key

◆ markNeedUpdate()

markNeedUpdate ( key)

Mark one key as needing update (stale).

Parameters
{string}key

◆ markNeedUpdateWhere()

markNeedUpdateWhere ( predicate)

Mark entries as needing update where the predicate matches.

Parameters
{function(string,{value: *, needUpdate: boolean, tags: (Object<string, *>|undefined) }): boolean} predicate

◆ set()

set ( key,
value,
options = {} )

Set a cache value.

Parameters
{string}key
{*}value
{Object}[options]
{(number|null)}[options.ttlMs]
{boolean}[options.needUpdate]
{Object<string,*>}[options.tags]