CacheWorker

CacheWorker

Cache instance for a worker process:

  • keeps local copy of a cache;
  • listens for updates from master;
  • notifies master about updates;
  • loads data from master when local copy misses a particular key.

Constructor

new CacheWorker(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

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

Source:

Remove a record from cache.

Parameters:
Name Type Description
group String

group key

id String

data key

Returns:

true if removed

Type
Boolean

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

Source:

Read a record from cache:

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

group key

id String

data key

Returns:

data if any, null otherwise

Type
Object

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

Source:

Remove a record from cache and database.

Parameters:
Name Type Description
group String

group key

id String

data key

Returns:

true if removed

Type
Boolean

start()

Source:

Start listening to IPC messages

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

Source:

Update data in the cache:

  • send an update to the master;
  • wait for a response with update status;
  • update the data in the local copy and return updated object in case of success, throw error otherwise.
Parameters:
Name Type Description
group String

group key

id String

data key

update Object

data to store

Returns:

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

Type
Object

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

Source:

Write data to cache:

  • send a write to the master;
  • wait for a response with write status;
  • write the data to local copy and return it in case of success, throw error otherwise.
Parameters:
Name Type Description
group String

group key

id String

data key

data Object

data to store

Returns:

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

Type
Object