Browse Source

Stores can override isCacheableValue + tests

This is usefull ie for redis cache where null is returned when cache data does not exists
In other stores, null can still be stored and undefined used when data does not exist

Each store can implement its own custom method
feature/fix-isCacheableValue-usage
Jonathan Muller 10 years ago
parent
commit
b579f8acb7
  1. 4
      examples/redis_example/redis_store.js
  2. 2
      lib/caching.js
  3. 21
      test/caching.unit.js

4
examples/redis_example/redis_store.js

@ -110,6 +110,10 @@ function redisStore(args) {
});
};
self.isCacheableValue = function(value) {
return value !== null && value !== undefined;
};
return self;
}

2
lib/caching.js

@ -34,6 +34,8 @@ var caching = function(args) {
if (typeof args.isCacheableValue === 'function') {
self._isCacheableValue = args.isCacheableValue;
} else if (typeof self.store.isCacheableValue === 'function') {
self._isCacheableValue = self.store.isCacheableValue;
} else {
self._isCacheableValue = function(value) {
return value !== undefined;

21
test/caching.unit.js

@ -707,4 +707,25 @@ describe("caching", function() {
});
});
});
describe("overloading with custom store", function() {
it("allows us to override isCacheableValue", function(done) {
var store = memoryStore.create({ttl: defaultTtl});
var onlyOne = true;
store.isCacheableValue = function(result) {
if (onlyOne) {
onlyOne = false;
done();
}
return result !== undefined;
};
cache = caching({store: store});
cache.wrap(key, function(cb) {
methods.getWidget(name, cb);
}, function(err, widget) {
checkErr(err);
assert.deepEqual(widget, {name: name});
});
});
});
});

Loading…
Cancel
Save