|
@ -1,3 +1,4 @@ |
|
|
|
|
|
/** @module cacheManager/caching */ |
|
|
/*jshint maxcomplexity:15*/ |
|
|
/*jshint maxcomplexity:15*/ |
|
|
var domain = require('domain'); |
|
|
var domain = require('domain'); |
|
|
var CallbackFiller = require('./callback_filler'); |
|
|
var CallbackFiller = require('./callback_filler'); |
|
@ -6,9 +7,7 @@ var CallbackFiller = require('./callback_filler'); |
|
|
* Generic caching interface that wraps any caching library with a compatible interface. |
|
|
* Generic caching interface that wraps any caching library with a compatible interface. |
|
|
* |
|
|
* |
|
|
* @param {object} args |
|
|
* @param {object} args |
|
|
* @param {object|string} args.store - The store must have at least the following functions: |
|
|
* @param {object|string} args.store - The store must at least have `set` and a `get` functions. |
|
|
* - set |
|
|
|
|
|
* - get |
|
|
|
|
|
* @param {function} [args.isCacheableValue] - A callback function which is called |
|
|
* @param {function} [args.isCacheableValue] - A callback function which is called |
|
|
* with every value returned from cache or from a wrapped function. This lets you specify |
|
|
* with every value returned from cache or from a wrapped function. This lets you specify |
|
|
* which values should and should not be cached. If the function returns true, it will be |
|
|
* which values should and should not be cached. If the function returns true, it will be |
|
@ -48,8 +47,15 @@ var caching = function(args) { |
|
|
* its results are stored in cache so subsequent calls retrieve from cache |
|
|
* its results are stored in cache so subsequent calls retrieve from cache |
|
|
* instead of calling the function. |
|
|
* instead of calling the function. |
|
|
* |
|
|
* |
|
|
* @example |
|
|
* @function |
|
|
|
|
|
* @name wrap |
|
|
|
|
|
* |
|
|
|
|
|
* @param {string} key - The cache key to use in cache operations |
|
|
|
|
|
* @param {function} work - The function to wrap |
|
|
|
|
|
* @param {object} [options] - options passed to `set` function |
|
|
|
|
|
* @param {function} cb |
|
|
* |
|
|
* |
|
|
|
|
|
* @example |
|
|
* var key = 'user_' + userId; |
|
|
* var key = 'user_' + userId; |
|
|
* cache.wrap(key, function(cb) { |
|
|
* cache.wrap(key, function(cb) { |
|
|
* User.get(userId, cb); |
|
|
* User.get(userId, cb); |
|
@ -100,26 +106,61 @@ var caching = function(args) { |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `get` function. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name get |
|
|
|
|
|
*/ |
|
|
self.get = self.store.get.bind(self.store); |
|
|
self.get = self.store.get.bind(self.store); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `set` function. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name set |
|
|
|
|
|
*/ |
|
|
self.set = self.store.set.bind(self.store); |
|
|
self.set = self.store.set.bind(self.store); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `del` function if it exists. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name del |
|
|
|
|
|
*/ |
|
|
if (typeof self.store.del === 'function') { |
|
|
if (typeof self.store.del === 'function') { |
|
|
self.del = self.store.del.bind(self.store); |
|
|
self.del = self.store.del.bind(self.store); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `setex` function if it exists. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name setex |
|
|
|
|
|
*/ |
|
|
if (typeof self.store.setex === 'function') { |
|
|
if (typeof self.store.setex === 'function') { |
|
|
self.setex = self.store.setex.bind(self.store); |
|
|
self.setex = self.store.setex.bind(self.store); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `reset` function if it exists. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name reset |
|
|
|
|
|
*/ |
|
|
if (typeof self.store.reset === 'function') { |
|
|
if (typeof self.store.reset === 'function') { |
|
|
self.reset = self.store.reset.bind(self.store); |
|
|
self.reset = self.store.reset.bind(self.store); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `keys` function if it exists. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name keys |
|
|
|
|
|
*/ |
|
|
if (typeof self.store.keys === 'function') { |
|
|
if (typeof self.store.keys === 'function') { |
|
|
self.keys = self.store.keys.bind(self.store); |
|
|
self.keys = self.store.keys.bind(self.store); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Binds to the underlying store's `ttl` function if it exists. |
|
|
|
|
|
* @function |
|
|
|
|
|
* @name ttl |
|
|
|
|
|
*/ |
|
|
if (typeof self.store.ttl === 'function') { |
|
|
if (typeof self.store.ttl === 'function') { |
|
|
self.ttl = self.store.ttl.bind(self.store); |
|
|
self.ttl = self.store.ttl.bind(self.store); |
|
|
} |
|
|
} |
|
|