CacheMaster

CacheMaster

Cache instance for master process:

  • (1) listen for requests from workers;
  • (2) listen for updates from capped collection;
  • send (1) to (2) and vice versa;
  • call group operators to read / write / udpate data from db / whatever.

Constructor

new CacheMaster(db, size)

Source:

Constructor

Parameters:
Name Type Default Description
db Mongo

database instance

size Number 100

max number of cache groups

Methods

cls(group) → {Object}

Source:

Just a handy method which returns an object with partials with given group.

Parameters:
Name Type Description
group String

group name

Returns:

object with all the {@code CacheWorker} methods without group

Type
Object

has(group, id) → {Object}

Source:

Check if local copy has data under the key.

Parameters:
Name Type Description
group String

group key

id String

data key

Returns:

data if any, undefined otherwise

Type
Object

init(group, age, size)

Source:

Register a set of operators for a particular cache group.

Parameters:
Name Type Description
group String

group key

options.init function

initializer - an "async () => [Object]" kind of function, preloads data to cache on startup

options.read function

reader - an "async (key) => Object" kind of function, returns data to cache if any for the key supplied

options.write function

writer - an "async (key, data) => Object" kind of function, persists the data cached if needed (must return the data persisted on success)

options.update function

updater - an "async (key, update) => Object" kind of function, updates persisted data if needed

options.remove function

remover - an "async (key) => Object" kind of function, removes persisted data if needed

age int

how long in ms to keep records in memory for the group

size int

how much records to keep in memory for the group

(async) purge(group, id, from) → {Boolean}

Source:

Remove a record from cache.

Parameters:
Name Type Default Description
group String

group key

id String

data key

from int 0

originating pid if any

Returns:

true if removed

Type
Boolean

(async) read(group, id, from) → {Object}

Source:

Read a record from cache:

  • from local copy if exists;
  • send a read request to master otherwise.
Parameters:
Name Type Default Description
group String

group key

id String

data key

from int 0

originating pid if any

Returns:

data if any, null otherwise

Type
Object

(async) remove(group, id, from) → {Boolean}

Source:

Remove a record from cache and database.

Parameters:
Name Type Description
group String

group key

id String

data key

from int

originating pid if any

Returns:

true if removed

Type
Boolean

start() → {Promise}

Source:

Attach to IPC & tailable cursor.

Returns:

with cursor open result

Type
Promise

stop()

Source:

Stop cursor.

(async) update(group, id, update, from) → {Object}

Source:

Update data in the cache

Parameters:
Name Type Default Description
group String

group key

id String

data key

update Object

data to store

from int 0

originating pid if any

Returns:

data if succeeded, null otherwise, throws in case of an error

Type
Object

(async) write(group, id, data, from) → {Object}

Source:

Write data to the cache & db

Parameters:
Name Type Default Description
group String

group key

id String

data key

data Object

data to store

from int 0

originating pid if any

Returns:

data if succeeded, null otherwise, throws in case of an error

Type
Object