Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cacheable

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cacheable - npm Package Compare versions

Comparing version 0.2.4 to 0.2.5

49

lib/cacheable.js
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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc