10 changed files with 184 additions and 264 deletions
@ -0,0 +1,91 @@ |
|||||
|
/** |
||||
|
* This is a very basic example of how you can implement your own Redis-based |
||||
|
* cache store with connection pooling. |
||||
|
*/ |
||||
|
|
||||
|
// npm install redis
|
||||
|
// npm install sol-redis-pool
|
||||
|
|
||||
|
var RedisPool = require('sol-redis-pool'); |
||||
|
|
||||
|
function redis_store(args) { |
||||
|
args = args || {}; |
||||
|
var self = {}; |
||||
|
var ttl = args.ttl; |
||||
|
self.name = 'redis'; |
||||
|
self.client = require('redis').createClient(args.port, args.host, args); |
||||
|
|
||||
|
var redis_options = { |
||||
|
redis_host: args.host || '127.0.0.1', |
||||
|
redis_port: args.port || 6379 |
||||
|
}; |
||||
|
|
||||
|
var pool = new RedisPool(redis_options); |
||||
|
|
||||
|
function connect(cb) { |
||||
|
pool.acquire(function (err, conn) { |
||||
|
if (err) { |
||||
|
pool.release(conn); |
||||
|
return cb(err); |
||||
|
} |
||||
|
|
||||
|
if (args.db) { |
||||
|
conn.select(args.db); |
||||
|
} |
||||
|
|
||||
|
cb(null, conn); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
self.get = function (key, cb) { |
||||
|
connect(function (err, conn) { |
||||
|
if (err) { return cb(err); } |
||||
|
|
||||
|
conn.get(key, function (err, result) { |
||||
|
if (err) { pool.release(conn); return cb(err); } |
||||
|
cb(null, JSON.parse(result)); |
||||
|
}); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
self.set = function (key, value, cb) { |
||||
|
connect(function (err, conn) { |
||||
|
if (err) { return cb(err); } |
||||
|
|
||||
|
if (ttl) { |
||||
|
conn.setex(key, ttl, JSON.stringify(value), function (err, result) { |
||||
|
pool.release(conn); |
||||
|
cb(err, result); |
||||
|
}); |
||||
|
} else { |
||||
|
conn.set(key, JSON.stringify(value), function (err, result) { |
||||
|
pool.release(conn); |
||||
|
cb(err, result); |
||||
|
}); |
||||
|
} |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
self.del = function (key, cb) { |
||||
|
connect(function (err, conn) { |
||||
|
if (err) { return cb(err); } |
||||
|
|
||||
|
conn.del(key, function (err, result) { |
||||
|
pool.release(conn); |
||||
|
cb(err, result); |
||||
|
}); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
return self; |
||||
|
} |
||||
|
|
||||
|
var methods = { |
||||
|
create: function (args) { |
||||
|
return redis_store(args); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
module.exports = methods; |
||||
|
|
||||
|
process.exit(); |
@ -1,44 +0,0 @@ |
|||||
/** |
|
||||
* Note: You very likely want to use your own Redis-based cache store instead |
|
||||
* of this one, especially for connection pooling. This is primarily an |
|
||||
* example implementation. |
|
||||
*/ |
|
||||
function redis_store(args) { |
|
||||
args = args || {}; |
|
||||
var self = {}; |
|
||||
var ttl = args.ttl; |
|
||||
self.name = 'redis'; |
|
||||
self.client = require('redis').createClient(args.port, args.host, args); |
|
||||
if (args.db) { |
|
||||
self.client.select(args.db); |
|
||||
} |
|
||||
|
|
||||
self.get = function (key, cb) { |
|
||||
self.client.get(key, function (err, result) { |
|
||||
cb(err, JSON.parse(result)); |
|
||||
}); |
|
||||
}; |
|
||||
|
|
||||
self.set = function (key, value, cb) { |
|
||||
if (ttl) { |
|
||||
self.client.setex(key, ttl, JSON.stringify(value), cb); |
|
||||
} else { |
|
||||
self.client.set(key, JSON.stringify(value), cb); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
self.del = function (key, cb) { |
|
||||
self.client.del(key, cb); |
|
||||
}; |
|
||||
|
|
||||
return self; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
var methods = { |
|
||||
create: function (args) { |
|
||||
return redis_store(args); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
module.exports = methods; |
|
@ -1,18 +0,0 @@ |
|||||
var support = require('../support'); |
|
||||
var redis_store = require('../../lib/stores/redis'); |
|
||||
|
|
||||
describe("redis store", function () { |
|
||||
describe("instantiating", function () { |
|
||||
it("lets us pass in a db arg", function (done) { |
|
||||
// Not sure how to prove that it uses the specified db in this test,
|
|
||||
// but it does.
|
|
||||
var redis_cache = redis_store.create({db: 2}); |
|
||||
support.test_set_get_del(redis_cache, done); |
|
||||
}); |
|
||||
|
|
||||
it("lets us pass in no args", function (done) { |
|
||||
var redis_cache = redis_store.create(); |
|
||||
support.test_set_get_del(redis_cache, done); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
Loading…
Reference in new issue