Browse Source

Adding Redis store example

hotfix/0.7.1
Bryan Donovan 12 years ago
parent
commit
a8c3982885
  1. 1
      Makefile
  2. 3
      README.md
  3. 47
      examples/redis_example/example.js
  4. 7
      examples/redis_example/redis_store.js

1
Makefile

@ -20,6 +20,7 @@ test-cov: cover check-coverage
lint: lint:
./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \ ./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc
./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc
.PHONY: test .PHONY: test

3
README.md

@ -44,6 +44,9 @@ node-cache-manager handles easily and transparently.
## Usage Examples ## Usage Examples
See examples below and in the examples directory. See ``examples/redis_example`` for an example of how to implement a
Redis cache store with connection pooling.
### Single Store ### Single Store
```javascript ```javascript

47
examples/redis_example/example.js

@ -0,0 +1,47 @@
// Setup:
// npm install redis
// npm install sol-redis-pool
var cache_manager = require('../../');
var redis_store = require('./redis_store');
var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100/*seconds*/});
redis_cache.set('foo', 'bar', function (err) {
if (err) { throw err; }
redis_cache.get('foo', function (err, result) {
console.log(result);
// >> 'bar'
redis_cache.del('foo', function (err) { console.log(err); });
});
});
function get_user(id, cb) {
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
}
var user_id = 123;
var key = 'user_' + user_id;
redis_cache.wrap(key, function (cb) {
get_user(user_id, cb);
}, function (err, user) {
console.log(user);
// Second time fetches user from redis_cache
redis_cache.wrap(key, function (cb) {
get_user(user_id, cb);
}, function (err, user) {
console.log(user);
});
});
// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
process.exit();

7
examples/redis_example/redis_client.js → examples/redis_example/redis_store.js

@ -3,9 +3,6 @@
* cache store with connection pooling. * cache store with connection pooling.
*/ */
// npm install redis
// npm install sol-redis-pool
var RedisPool = require('sol-redis-pool'); var RedisPool = require('sol-redis-pool');
function redis_store(args) { function redis_store(args) {
@ -29,7 +26,7 @@ function redis_store(args) {
return cb(err); return cb(err);
} }
if (args.db) { if (args.db || args.db === 0) {
conn.select(args.db); conn.select(args.db);
} }
@ -87,5 +84,3 @@ var methods = {
}; };
module.exports = methods; module.exports = methods;
process.exit();
Loading…
Cancel
Save