DataCache

DataCache

Implements data cache mechanisms in MOST Data Applications. DataCache class is used as the internal data caching engine, if any other caching mechanism is not defined.

Constructor

new DataCache()

Properties:
Name Type Description
ttl Number An amount of time in seconds which is the default cached item lifetime.
Source:

Extends

  • EventEmitter2

Methods

add(key, value, ttlopt, callbackopt)

Sets a key value pair in cache.
Parameters:
Name Type Attributes Description
key string A string that represents the key of the cached value
value * The value to be cached
ttl number <optional>
A TTL in seconds. This parameter is optional.
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured and the second argument will return true on success.
Source:
Example
var d = require("most-data");
 d.cache.current.add('/User/100', { "id":100,"name":"user1@example.com","description":"User #1" }, 1200, function(err) {
    done(err);
 });

ensure(key, fn, callback)

Gets data from cache or executes the defined function and adds the result to the cache with the specified key
Parameters:
Name Type Description
key string | * A string thath represents the of the cached data
fn function A function to execute if data will not be found in cache
callback function A callback function where the first argument will contain the Error object if an error occured and the second argument will contain the result.
Source:
Example
var d = require("most-data");
 //try to find user with id 100 in cache
 d.cache.current.ensure('/User/100', function(cb) {
        //otherwise get user from database
      context.model('User').where('id').equal(100).first().then(function(result) {
        cb(null, result);
      }).catch(function(err) {
        cb(err);
      }
 }, function(err, result) {
    //and finally return the result
    done(err,result);
 };

get(key, callback)

Gets a cached value defined by the given key.
Parameters:
Name Type Description
key string | * A string that represents the key of the cached value
callback function A callback function where the first argument will contain the Error object if an error occured and the second argument will contain the result.
Source:
Example
var d = require("most-data");
 //try to find article with id 100 in cache
 d.cache.current.get('/Article/100', function(err, result) {
    done(err,result);
 };

init(callback)

Initializes data caching.
Parameters:
Name Type Description
callback function A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:
Example
var d = require("most-data");
 //try to find article with id 100 in cache
 d.cache.current.init(function(err) {
    done(err);
 };

remove(key, callbackopt)

Removes a cached value.
Parameters:
Name Type Attributes Description
key string A string that represents the key of the cached value
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:
Example
var d = require("most-data");
 //try to find article with id 100 in cache
 d.cache.current.remove('/Article/100', function(err) {
    done(err);
 };

removeAll(callbackopt)

Flush all cached data.
Parameters:
Name Type Attributes Description
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:
Example
var d = require("most-data");
 //try to find article with id 100 in cache
 d.cache.current.removeAll(function(err) {
    done(err);
 };