Comparing version 0.2.4 to 0.2.5
var Storeman = require('storeman'); | ||
var keyf = require('keyf') | ||
var debug = require('debug')('cacheable:debug') | ||
@@ -47,2 +48,6 @@ | ||
* Wraps an async funtion, automaticly cache the result | ||
* | ||
* The async function must have a signature of `fn(arg1, [arg2...,] callback)`. | ||
* The `arg1` can be an options object, if `options.fresh` is passed as true, | ||
* cache will not be used. | ||
*/ | ||
@@ -92,2 +97,8 @@ Cacheable.prototype.wrap = function(fn, key, ttl, ctx) { | ||
} | ||
// when `options.fresh` is passed as true, | ||
// don't use cache | ||
if ('object' == typeof args[0] && args[0].fresh) { | ||
return run() | ||
} | ||
// cache key is not fully formatted means some args are not passed in | ||
if (_key.match(RE_KEY_PATTERN)) { | ||
@@ -117,2 +128,3 @@ debug('cache key not fully formatted, skip "%s"', _key, args) | ||
} | ||
// make sure we save cache after callback | ||
args[args.length - 1] = function(err, result) { | ||
@@ -133,6 +145,2 @@ function _done(){ | ||
function isdigit(s) { | ||
return !isNaN(Number(s)) | ||
} | ||
/** | ||
@@ -142,31 +150,8 @@ * How to replace a cache key | ||
Cacheable.keyReplacer = function(key, fn, args) { | ||
var ks, v, k | ||
var self = this | ||
return key.replace(RE_KEY_PATTERN, function(m, format, p1) { | ||
ks = p1.split('.') | ||
v = null | ||
k = ks.shift() | ||
if (k === 'this') { | ||
v = self | ||
} else if (k === '_fn_') { | ||
v = fn[_REALNAME] | ||
} else if (k === '_model_') { | ||
v = self[_REALNAME] | ||
} else { | ||
v = isdigit(k) ? args[k] : self[k] | ||
} | ||
// dive into object | ||
while (v && ks.length && ks[0] in v) { | ||
v = v[ks.shift()] | ||
} | ||
// make the value jsonized | ||
if (format === '%j') { | ||
v = JSON.stringify(v) | ||
} | ||
// a `[object object]` should not be used | ||
if ('object' === typeof v) { | ||
v = null | ||
} | ||
return v || m | ||
}) | ||
var data = { | ||
_fn_: fn && fn[_REALNAME], | ||
_model_: self[_REALNAME] | ||
} | ||
return keyf(key, data).call(self, args) | ||
} | ||
@@ -173,0 +158,0 @@ |
{ | ||
"name": "cacheable", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "A cache wrapper with redis", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./node_modules/bin/mocha" | ||
}, | ||
"dependencies": { | ||
"debug": "0.7.x", | ||
"keyf": "~0.0.1", | ||
"storeman": "~0.0.1" | ||
@@ -12,0 +13,0 @@ }, |
@@ -67,2 +67,4 @@ # Cacheable | ||
// enable cache for `User.get` method | ||
// So when you call `User.get(some_id)`, it will fetch data | ||
// from cache first, when cache not found, then the original function will be called. | ||
User.enableCache('get', '{_model_}:{0}') // '{0}' means the `arguments[0]` | ||
@@ -69,0 +71,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
13794
2
191
0
3
276
+ Addedkeyf@~0.0.1
+ Addedkeyf@0.0.2(transitive)