Browse Source

refactored redis example store, added ttl function to caching.js

feature/specify-what-to-cache
Bryan Donovan 10 years ago
parent
commit
c2eac33895
  1. 25
      examples/redis_example/example.js
  2. 51
      examples/redis_example/redis_store.js
  3. 4
      lib/caching.js
  4. 22
      test/caching.unit.js

25
examples/redis_example/example.js

@ -3,6 +3,7 @@
// node examples/redis_example/example.js
var util = require('util');
var assert = require('assert');
var cacheManager = require('../../');
var redisStore = require('./redis_store');
// Note: ttl is in seconds
@ -18,8 +19,14 @@ redisCache.set('foo', 'bar', {ttl: ttl}, function(err) {
if (err) { throw err; }
console.log("result fetched from cache: " + result);
// >> 'bar'
redisCache.del('foo', function(err) {
redisCache.ttl('foo', function(err, result) {
if (err) { throw err; }
assert.ok(result > 59 && result < 61);
redisCache.del('foo', function(err) {
if (err) { throw err; }
});
});
});
});
@ -32,8 +39,14 @@ redisCache.set('foo-no-ttl', 'bar-no-ttl', function(err) {
if (err) { throw err; }
console.log("result fetched from cache: " + result);
// >> 'bar'
redisCache.del('foo-no-ttl', function(err) {
redisCache.ttl('foo-no-ttl', function(err, result) {
if (err) { throw err; }
assert.ok(result > 99 && result < 101);
redisCache.del('foo-no-ttl', function(err) {
if (err) { throw err; }
});
});
});
});
@ -46,8 +59,14 @@ redisCache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) {
if (err) { throw err; }
console.log("result fetched from cache: " + result);
// >> 'bar'
redisCache.del('foo-zero-ttl', function(err) {
redisCache.ttl('foo-zero-ttl', function(err, result) {
if (err) { throw err; }
assert.ok(result < 0);
redisCache.del('foo-zero-ttl', function(err) {
if (err) { throw err; }
});
});
});
});

51
examples/redis_example/redis_store.js

@ -33,6 +33,22 @@ function redisStore(args) {
});
}
function handleResponse(conn, cb, opts) {
opts = opts || {};
return function(err, result) {
pool.release(conn);
if (err) { return cb(err); }
if (opts.parse) {
result = JSON.parse(result);
}
cb(null, result);
};
}
self.get = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
@ -40,12 +56,7 @@ function redisStore(args) {
connect(function(err, conn) {
if (err) { return cb(err); }
conn.get(key, function(err, result) {
pool.release(conn);
if (err) { return cb(err); }
cb(null, JSON.parse(result));
});
conn.get(key, handleResponse(conn, cb, {parse: true}));
});
};
@ -60,17 +71,12 @@ function redisStore(args) {
connect(function(err, conn) {
if (err) { return cb(err); }
var val = JSON.stringify(value);
if (ttl) {
conn.setex(key, ttl, JSON.stringify(value), function(err, result) {
pool.release(conn);
cb(err, result);
});
conn.setex(key, ttl, val, handleResponse(conn, cb));
} else {
conn.set(key, JSON.stringify(value), function(err, result) {
pool.release(conn);
cb(err, result);
});
conn.set(key, val, handleResponse(conn, cb));
}
});
};
@ -78,11 +84,14 @@ function redisStore(args) {
self.del = function(key, cb) {
connect(function(err, conn) {
if (err) { return cb(err); }
conn.del(key, handleResponse(conn, cb));
});
};
conn.del(key, function(err, result) {
pool.release(conn);
cb(err, result);
});
self.ttl = function(key, cb) {
connect(function(err, conn) {
if (err) { return cb(err); }
conn.ttl(key, handleResponse(conn, cb));
});
};
@ -94,11 +103,7 @@ function redisStore(args) {
connect(function(err, conn) {
if (err) { return cb(err); }
conn.keys(pattern, function(err, result) {
pool.release(conn);
cb(err, result);
});
conn.keys(pattern, handleResponse(conn, cb));
});
};

4
lib/caching.js

@ -95,6 +95,10 @@ var caching = function(args) {
self.keys = self.store.keys.bind(self.store);
}
if (typeof self.store.ttl === 'function') {
self.ttl = self.store.ttl.bind(self.store);
}
return self;
};

22
test/caching.unit.js

@ -194,6 +194,28 @@ describe("caching", function() {
});
});
describe("ttl()", function() {
var fakeStore;
beforeEach(function() {
fakeStore = {
get: function() {},
set: function() {},
del: function() {},
ttl: function() {}
};
sinon.stub(fakeStore, 'ttl');
cache = caching({store: fakeStore});
});
it("passes the params to the underlying store's ttl() method", function() {
cache.ttl('foo');
assert.ok(fakeStore.ttl.calledWith('foo'));
});
});
describe("keys()", function() {
var keyCount;
var savedKeys = [];

Loading…
Cancel
Save