From a8c3982885ef0e306ef6467f6464731f7aea542b Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sun, 13 Oct 2013 18:10:16 -0700 Subject: [PATCH] Adding Redis store example --- Makefile | 1 + README.md | 3 ++ examples/redis_example/example.js | 47 +++++++++++++++++++ .../{redis_client.js => redis_store.js} | 7 +-- 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 examples/redis_example/example.js rename examples/redis_example/{redis_client.js => redis_store.js} (95%) diff --git a/Makefile b/Makefile index c35c57d..c8be13b 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ test-cov: cover check-coverage lint: ./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \ ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc + ./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc .PHONY: test diff --git a/README.md b/README.md index ff10812..a27fbc4 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ node-cache-manager handles easily and transparently. ## 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 ```javascript diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js new file mode 100644 index 0000000..43f6f94 --- /dev/null +++ b/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(); diff --git a/examples/redis_example/redis_client.js b/examples/redis_example/redis_store.js similarity index 95% rename from examples/redis_example/redis_client.js rename to examples/redis_example/redis_store.js index 684c225..436d54b 100644 --- a/examples/redis_example/redis_client.js +++ b/examples/redis_example/redis_store.js @@ -3,9 +3,6 @@ * cache store with connection pooling. */ -// npm install redis -// npm install sol-redis-pool - var RedisPool = require('sol-redis-pool'); function redis_store(args) { @@ -29,7 +26,7 @@ function redis_store(args) { return cb(err); } - if (args.db) { + if (args.db || args.db === 0) { conn.select(args.db); } @@ -87,5 +84,3 @@ var methods = { }; module.exports = methods; - -process.exit();