Comparing version 0.2.6 to 0.2.7
@@ -20,13 +20,10 @@ var Storeman = require('storeman'); | ||
* - `prefix` prefix for all keys under this cached manager | ||
* - `silent` to ignore `redis.get` error | ||
* - `silent` to ignore `storage.get` error | ||
* | ||
* | ||
*/ | ||
function Cacheable(options) { | ||
if (!(this instanceof Cacheable)) return new Cacheable(options) | ||
options = options || {} | ||
if (!options.prefix && options.prefix !== '') { | ||
options.prefix = 'cached:' | ||
} | ||
this.prefix = options.prefix | ||
this.silent = options.silent === false ? false : true | ||
@@ -40,4 +37,4 @@ this.helpers = {} | ||
Cacheable.prototype.debug = debug | ||
Cacheable.prototype.DEFAULT_KEY = '{_model_}:{_fn_}:%j{0}' | ||
Cacheable.prototype.DEFAULT_PROTO_KEY = '{_model_}:{id}:{_fn_}' | ||
// default key for wrapping standalone functions | ||
Cacheable.prototype.DEFAULT_KEY = '{_fn_}:%j{0}' | ||
@@ -71,4 +68,4 @@ Cacheable.prototype._applykey = function applykey(ctx, key, fn, args) { | ||
if (!fn[_REALNAME] && key.indexOf('{method}')) { | ||
throw new Error('Cache key referred to "{method}", but the function doesn\'t have a name') | ||
if (!getName(fn) && ~key.indexOf('{_fn_}')) { | ||
throw new Error('Cache key referred to "{_fn_}", but the function doesn\'t have a name') | ||
} | ||
@@ -79,3 +76,3 @@ | ||
if (uniqkey in allkeys) { | ||
log('Possible key conflict -> fn: [%s], key: %s', fn[_REALNAME], uniqkey) | ||
log('Possible key conflict -> fn: [%s], key: %s', getName(fn), uniqkey) | ||
} else { | ||
@@ -85,3 +82,3 @@ allkeys[uniqkey] = null | ||
debug('wrapping "%s" - "%s", ttl: %s', fn[_REALNAME] || '[anonymous]', uniqkey, ttl) | ||
debug('wrapping `%s`, key: "%s", ttl: %s', getName(fn) || '[anonymous]', uniqkey, ttl) | ||
@@ -108,3 +105,3 @@ return function cacheWrapped() { | ||
if (_key.match(RE_KEY_PATTERN)) { | ||
debug('cache key not fully formatted, skip "%s"', _key, args) | ||
debug('cache key not fully formatted, skip "%s"', _key) | ||
return run() | ||
@@ -149,2 +146,9 @@ } | ||
/** | ||
* Get function's realname | ||
*/ | ||
function getName(fn) { | ||
return fn[_REALNAME] || fn.name | ||
} | ||
/** | ||
* How to replace a cache key | ||
@@ -155,3 +159,3 @@ */ | ||
var data = { | ||
_fn_: fn && fn[_REALNAME], | ||
_fn_: fn && getName(fn), | ||
_model_: self[_REALNAME] | ||
@@ -158,0 +162,0 @@ } |
@@ -9,2 +9,6 @@ // Global cache for constructor classes | ||
// default keys for wrapping model methods | ||
var DEFAULT_KEY = '{_model_}:{_fn_}' | ||
var DEFAULT_PROTO_KEY = '{_model_}:{id}:{_fn_}' | ||
function enableCache(cached) { | ||
@@ -14,3 +18,3 @@ function enableInstCache(method, key, ttl) { | ||
var fn = isValidFunc(cls.prototype[method], method) | ||
key = key || cached.DEFAULT_PROTO_KEY.replace('{_fn_}', method) | ||
key = key || DEFAULT_PROTO_KEY.replace('{_fn_}', method) | ||
hiddenProperty(fn, _REALNAME, fn.name || method) | ||
@@ -33,3 +37,3 @@ cls.addCacheKey(key) | ||
fn = isValidFunc(cls[method], method) | ||
key = key || cached.DEFAULT_KEY.replace('{_fn_}', method) | ||
key = key || DEFAULT_KEY.replace('{_fn_}', method) | ||
hiddenProperty(fn, _REALNAME, fn.name || method) | ||
@@ -36,0 +40,0 @@ cls['fresh_' + method] = fn |
{ | ||
"name": "cacheable", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "A cache wrapper with redis", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/bin/mocha" | ||
"test": "./node_modules/.bin/mocha" | ||
}, | ||
@@ -12,6 +12,9 @@ "dependencies": { | ||
"keyf": "~0.0.1", | ||
"storeman": "~0.0.1" | ||
"storeman": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "*", | ||
"should": "*", | ||
"lru-cache": "*", | ||
"istanbul": "*" | ||
}, | ||
@@ -18,0 +21,0 @@ "repository": { |
@@ -131,3 +131,3 @@ # Cacheable | ||
{_model_}:{_fn_}:%j{0} | ||
{_model_}:{_fn_} | ||
@@ -134,0 +134,0 @@ `{_fn_}` is the name of the function `fn`. If not found, an error will throw. |
Sorry, the diff of this file is not supported yet
17391
10
391
4
+ Addedstoreman@0.1.2(transitive)
- Removedstoreman@0.0.3(transitive)
Updatedstoreman@~0.1.1