Comparing version 0.2.0 to 0.2.4
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib/cached.js'); | ||
module.exports = require('./lib/cacheable.js'); |
@@ -5,5 +5,5 @@ // Global cache for constructor classes | ||
var Cached = require('./cached') | ||
var Cacheable = require('./cacheable') | ||
var _REALNAME = Cached._REALNAME | ||
var _REALNAME = Cacheable._REALNAME | ||
@@ -13,3 +13,3 @@ function enableCache(cached) { | ||
var cls = this | ||
var fn = validFunc(cls.prototype[method], method) | ||
var fn = isValidFunc(cls.prototype[method], method) | ||
key = key || cached.DEFAULT_PROTO_KEY.replace('{_fn_}', fn[_REALNAME]) | ||
@@ -26,3 +26,3 @@ hiddenProperty(fn, _REALNAME, fn.name || method) | ||
} | ||
fn = validFunc(cls[method], method) | ||
fn = isValidFunc(cls[method], method) | ||
key = key || cached.DEFAULT_KEY.replace('{_fn_}', fn[_REALNAME]) | ||
@@ -51,6 +51,40 @@ hiddenProperty(fn, _REALNAME, fn.name || method) | ||
Cached.prototype._initClassHelper = function() { | ||
/** | ||
* Revive object from a json string | ||
*/ | ||
function reviver(k, v) { | ||
if ('object' == typeof v && v !== null && !Array.isArray(v)) { | ||
var cls = v[_REALNAME] | ||
if (cls) { | ||
if (!(cls in constructors)) { | ||
log('Constructor for %s doesn\'t exist anymore.', cls) | ||
// return a undefined, mean this object is not available anymore | ||
return | ||
} | ||
cls = constructors[cls] | ||
delete v[_REALNAME] | ||
v = new cls(v) | ||
if (v._unpickle) { | ||
v._unpickle() | ||
} | ||
} | ||
return v | ||
} | ||
if ('string' === typeof v && RE_JSON_TIME.test(v)) { | ||
// revive a date object | ||
v = new Date(v) | ||
} | ||
return v | ||
} | ||
function unpickle(value) { | ||
return JSON.parse(value, reviver) | ||
} | ||
Cacheable.prototype._initClassHelper = function() { | ||
this.helpers.enableCache = enableCache(this) | ||
this.helpers.clearCache = clearCache(this) | ||
this.helpers.addCacheKey = addCacheKey | ||
this.unpickle = unpickle | ||
} | ||
@@ -61,3 +95,3 @@ | ||
*/ | ||
Cached.prototype.register = function(cls, modelName) { | ||
Cacheable.prototype.register = function(cls, modelName) { | ||
modelName = modelName || cls.modelName || cls.name | ||
@@ -80,3 +114,3 @@ hiddenProperty(cls, _REALNAME, modelName) | ||
*/ | ||
Cached.prototype._extend = function extendClass(cls) { | ||
Cacheable.prototype._extend = function extendClass(cls) { | ||
var cached = this, proto = cls.prototype | ||
@@ -111,14 +145,8 @@ cls.classCacheKeys = [] | ||
Cached.prototype.pickle = function pickle(value) { | ||
return JSON.stringify(value) | ||
} | ||
Cached.prototype.unpickle = function unpickle(value) { | ||
return JSON.parse(value, reviver) | ||
function hiddenProperty(where, property, value) { | ||
Object.defineProperty(where, property, { value: value }); | ||
} | ||
/** | ||
* Constructor function helpers | ||
*/ | ||
function validFunc(fn, method) { | ||
function isValidFunc(fn, method) { | ||
if (!fn) { | ||
@@ -134,34 +162,1 @@ throw new Error('method "' + method + '" not defined') | ||
/** | ||
* How to stringify an object when load data from redis | ||
*/ | ||
function reviver(k, v) { | ||
if (k === '' && v && 'object' == typeof v) { | ||
var cls = v[_REALNAME] | ||
if (cls) { | ||
if (!(cls in constructors)) { | ||
log('Constructor for %s doesn\'t exist anymore.', cls) | ||
// return a undefined, mean this object is not available anymore | ||
return | ||
} | ||
cls = constructors[cls] | ||
delete v[_REALNAME] | ||
v = new cls(v) | ||
if (v._unpickle) { | ||
v._unpickle() | ||
} | ||
} | ||
return v | ||
} | ||
if ('string' === typeof v && RE_JSON_TIME.test(v)) { | ||
// revive a date object | ||
v = new Date(v) | ||
} | ||
return v | ||
} | ||
function hiddenProperty(where, property, value) { | ||
Object.defineProperty(where, property, { value: value }); | ||
} | ||
{ | ||
"name": "cacheable", | ||
"version": "0.2.0", | ||
"version": "0.2.4", | ||
"description": "A cache wrapper with redis", | ||
@@ -10,4 +10,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "0.7.x" | ||
"debug": "0.7.x", | ||
"storeman": "~0.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
}, | ||
"repository": { | ||
@@ -14,0 +18,0 @@ "type": "git", |
@@ -185,9 +185,5 @@ # Cacheable | ||
## TODO | ||
Make this package compatible with other cache backend, rather than only redis right now. | ||
## License | ||
the MIT licence. |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
13752
2
1
290
189
1
+ Addedstoreman@~0.0.1
+ Addedafter@0.8.2(transitive)
+ Addedstoreman@0.0.3(transitive)