Cache

File-system backed cache helper with TTL-based invalidation.

Constructor

new Cache(app)

Constructor.

Parameters:
NameTypeDescription
appobject

Application container

Methods

_purgeItem(name) → {void}

Remove a specific cache namespace or all first-level namespaces.

Parameters:
NameTypeDescription
namestring

Namespace name or * for all

Returns:
Type: 
void

(async) dispose() → {Promise.<void>}

Dispose internal reference.

Returns:
Type: 
Promise.<void>

getRootDir() → {string}

Get root directory for this class. It will be used to store all cache files, organized by namespace and TTL.

Returns:

Absolute cache root directory

Type: 
string

load(name, ttlDuropt) → {Promise.<any>}

Load cached content when available and not expired.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Cache key in namespaced path format

ttlDurnumber | string<optional>
0

TTL duration (milliseconds or parseable duration)

Returns:

Cached value, or undefined if missing/expired

Type: 
Promise.<any>

prep(name, ttlDuropt) → {Object|undefined}

Prepare cache paths for a namespaced key and TTL.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Cache key in namespaced path format

ttlDurnumber | string<optional>
0

TTL duration (milliseconds or parseable duration)

Returns:

Prepared paths or undefined when not cacheable

Type: 
Object | undefined

purge(nameopt) → {void}

Purge cache by namespace or remove expired TTL directories.

Parameters:
NameTypeAttributesDescription
namestring<optional>

Optional namespace to remove directly

Returns:
Type: 
void

save(name, item, ttlDuropt) → {Promise.<void>}

Save a value into cache for the given key and TTL directory.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Cache key in namespaced path format

itemany

Value to persist

ttlDurnumber | string<optional>
0

TTL duration (milliseconds or parseable duration)

Returns:

Resolves when the value is written

Type: 
Promise.<void>

sync(name, item, ttlDuropt) → {Promise.<any>}

Return cached content, and store the fallback value if cache is empty.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Cache key in namespaced path format

itemany

Fallback value to save when cache miss happens

ttlDurnumber | string<optional>
0

TTL duration (milliseconds or parseable duration)

Returns:

Cached content

Type: 
Promise.<any>