Class: Dekache

Dekache

const mycache = new Dekache({name:'you got any cache?', mins: 2})
await mycache.get('some-identifier', async() => { return 1 });

mycache.get('some-identifier', ()=>{}) calls will now return a promise that resolves to 1 until the number of mins (2) has been reached.

new Dekache ()

Members

data

Properties:
Name Type Description
data object

stores key value pairs for cache items

Methods

clear (forced) async

Iterates over each cache item and clears any that have overstayed the duration.

Name Type Default Description
forced boolean false optional

Should all cache items be forced out regardless of duration?

delete (data_key)boolean async

Delete the given key from the cache

Name Type Description
data_key object

the identifier for the cache item to delte.

Returns:
Type Description
boolean Based on if any item was deleted from cache. False if not.

get (data_key, callback)promise

Get the data, or uses callback function to populate cache and then returns data.

Name Type Description
data_key object

the identifier for the cache item to get.

callback function

an asyncronous callback that should be called if no data is currently in the cache.

Returns:
Type Description
promise Based on the data returned from the first time that this function was called and the callbacks return data.

initialize (options)

Called automatically when created

Name Type Description
options Dekache~options

a cache options object.

key (data_key)string

Gets the internal key from data_id. Can be used for comparing an id when a cache item is cleared.

Name Type Description
data_key object

the identifier for the cache item to get.

Returns:
Type Description
string the internal cache key used

loop () async

The main cache loop function. Calls Dekache#clear .

set (data_key, new_data)promise

Set the cache data to new data directly and then returns promise.

Name Type Description
data_key object

the identifier for the cache item to get.

new_data object

some object or primative.

Returns:
Type Description
promise that resolves with new_data

start ()boolean async

Starts the cache loop. Can be later stopped called Dekache#stop

Returns:
Type Description
boolean Based on if started. False if already started.

stop ()boolean async

Stops the cache loop. Can later be restarted calling Dekache#start

Returns:
Type Description
boolean Based on if stopped. False if already stopped.

Type Definitions

options

An object containing key value pairs where the key is a string identifier, and the value is an object with the properties detailed below:

Properties:
Name Type Argument Default Description
name string <optional>
''

An identifyer for this cache.

type string <optional>
force

the cache type, either 'force' or 'renew'.

mins number <optional>
1

Number of minutes to cache each item for

freq number <optional>
1000

The frequency to check cache items for deletion (ms)