Cache

Simple, file-system based cache with TTL-based invalidation for every day use.

Constructor

new Cache(app)

Constructor.

Parameters:
NameTypeDescription
appApp

App instance

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. 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.<*>}

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.<*>

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

item*

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.<*>}

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

Parameters:
NameTypeAttributesDefaultDescription
namestring

Cache key in namespaced path format

item*

Fallback value to save when cache miss happens

ttlDurnumber | string<optional>
0

TTL duration (milliseconds or parseable duration)

Returns:

Cached content

Type: 
Promise.<*>