Comparing version 0.2.7 to 0.2.9
@@ -0,2 +1,4 @@ | ||
// use strict; | ||
// Global cache for constructor classes | ||
var constructors = {} | ||
@@ -10,4 +12,5 @@ var RE_JSON_TIME = /^[0-9\-]+T[0-9\:\.]+Z$/ | ||
// default keys for wrapping model methods | ||
var DEFAULT_KEY = '{_model_}:{_fn_}' | ||
var DEFAULT_KEY = '{_model_}:{_fn_}:%j{0}' | ||
var DEFAULT_PROTO_KEY = '{_model_}:{id}:{_fn_}' | ||
var ERROR_DUPLICATE_REGISTRY = 'DUPLICATE_REGISTRY' | ||
@@ -63,3 +66,3 @@ function enableCache(cached) { | ||
function reviver(k, v) { | ||
if ('object' == typeof v && v !== null && !Array.isArray(v)) { | ||
if (v && v[_REALNAME]) { | ||
var cls = v[_REALNAME] | ||
@@ -73,2 +76,3 @@ if (cls) { | ||
cls = constructors[cls] | ||
// remove `__cachedname` assigned to this object | ||
delete v[_REALNAME] | ||
@@ -98,3 +102,3 @@ v = new cls(v) | ||
this.helpers.addCacheKey = addCacheKey | ||
this.unpickle = unpickle | ||
this._decode = unpickle | ||
} | ||
@@ -110,3 +114,5 @@ | ||
if (cls[_REALNAME] in constructors) { | ||
throw new Error('Class "' + cls[_REALNAME] + '" already defined') | ||
var err = new Error('Class "' + cls[_REALNAME] + '" already defined') | ||
err.code = ERROR_DUPLICATE_REGISTRY | ||
throw err | ||
} | ||
@@ -113,0 +119,0 @@ if (!this.helpers.enableCache) { |
{ | ||
"name": "cacheable", | ||
"version": "0.2.7", | ||
"version": "0.2.9", | ||
"description": "A cache wrapper with redis", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"keyf": "~0.0.1", | ||
"storeman": "~0.1.1" | ||
"storeman": "~0.1.2" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
var should = require('should') | ||
var LRU = require('lru-cache') | ||
var User = require('./support/user') | ||
@@ -117,3 +118,2 @@ var Cacheable = require('../') | ||
} | ||
}) | ||
@@ -126,2 +126,19 @@ | ||
describe('register', function() { | ||
it('cannot register twice', function() { | ||
var err | ||
cache.register(User) | ||
try { | ||
cache.register(User) | ||
} catch (e) { | ||
err = e | ||
} | ||
should.exist(err) | ||
err.code.should.equal('DUPLICATE_REGISTRY') | ||
}) | ||
}) | ||
}) | ||
@@ -131,2 +148,51 @@ | ||
describe('class method', function() { | ||
it('should cache', function(done) { | ||
User.enableCache('get') | ||
User.get(1, function(err, result) { | ||
should.equal(User._called, true) | ||
User._called = 'haha' | ||
User.get(1, function(err, result) { | ||
// will remain haha, if cache is used | ||
should.equal(User._called, 'haha') | ||
result.should.be.instanceOf(User) | ||
should.equal(result.data.id, 1) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('instance method', function() { | ||
var user = new User({ id: 1 }) | ||
it('should cache', function(done) { | ||
User.enableCache('.articleIds') | ||
user.articleIds(function(err, result) { | ||
User._called = 'instance' | ||
user.articleIds(function(err, result) { | ||
result.should.eql([1,2,3,1]) | ||
should.equal(User._called, 'instance') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('can delete cache', function(done) { | ||
user.articleIds(function(err, result) { | ||
should.equal(User._called, 'instance') | ||
user._clearCache(function() { | ||
user.articleIds(function(err, result) { | ||
should.equal(User._called, true) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
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
19592
11
472
Updatedstoreman@~0.1.2