Skip to content

Commit 1e9e183

Browse files
committed
Merge branch 'release/0.1.0' into develop
2 parents 53bc2b3 + a8c3982 commit 1e9e183

File tree

12 files changed

+230
-264
lines changed

12 files changed

+230
-264
lines changed

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
- 0.1.0 2013-10-13
2+
Removing built-in Redis store to emphasize that you should plug in your own
3+
cache store.
4+
15
- 0.0.5 2013-10-13
26
Removing hiredis requirement.
37

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test-cov: cover check-coverage
2020
lint:
2121
./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
2222
./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc
23+
./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc
2324

2425

2526
.PHONY: test

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ priority cache(s) first.
2222

2323
## Overview
2424

25-
First, node-cache-manager features the standard functions you'd expect in most caches:
25+
First, it includes a `wrap` function that lets you wrap any function in cache.
26+
(Note, this was inspired by [node-caching](https://github.com/mape/node-caching).)
27+
28+
Second, node-cache-manager features a built-in memory cache (using [node-lru-cache](https://github.com/isaacs/node-lru-cache)),
29+
with the standard functions you'd expect in most caches:
2630

2731
set(key, val, cb)
2832
get(key, cb)
2933
del(key, cb)
3034

31-
Second, it includes a `wrap` function that lets you wrap any function in cache.
32-
(Note, this was inspired by [node-caching](https://github.com/mape/node-caching).)
33-
3435
Third, node-cache-manager lets you set up a tiered cache strategy. This may be of
3536
limited use in most cases, but imagine a scenario where you expect tons of
36-
traffic, and don't want to hit Redis for every request. You decide to store
37-
the most commonly-requested data in an in-memory cache (like [node-lru-cache](https://github.com/isaacs/node-lru-cache)),
37+
traffic, and don't want to hit your primary cache (like Redis) for every request.
38+
You decide to store the most commonly-requested data in an in-memory cache,
3839
perhaps with a very short timeout and/or a small data size limit. But you
3940
still want to store the data in Redis for backup, and for the requests that
4041
aren't as common as the ones you want to store in memory. This is something
@@ -43,22 +44,24 @@ node-cache-manager handles easily and transparently.
4344

4445
## Usage Examples
4546

47+
See examples below and in the examples directory. See ``examples/redis_example`` for an example of how to implement a
48+
Redis cache store with connection pooling.
49+
4650
### Single Store
4751

4852
```javascript
4953
var cache_manager = require('cache-manager');
50-
var redis_cache = cache_manager.caching({store: 'redis', db: 1, ttl: 100/*seconds*/});
5154
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
5255

5356
// Note: callback is optional in set() and del().
5457

55-
redis_cache.set('foo', 'bar', function(err) {
58+
memory_cache.set('foo', 'bar', function(err) {
5659
if (err) { throw err; }
5760

58-
redis_cache.get('foo', function(err, result) {
61+
memory_cache.get('foo', function(err, result) {
5962
console.log(result);
6063
// >> 'bar'
61-
redis_cache.del('foo', function(err) {});
64+
memory_cache.del('foo', function(err) {});
6265
});
6366
});
6467

@@ -72,13 +75,13 @@ node-cache-manager handles easily and transparently.
7275
var user_id = 123;
7376
var key = 'user_' + user_id;
7477

75-
redis_cache.wrap(key, function (cb) {
78+
memory_cache.wrap(key, function (cb) {
7679
get_user(user_id, cb);
7780
}, function (err, user) {
7881
console.log(user);
7982

80-
// Second time fetches user from redis_cache
81-
redis_cache.wrap(key, function (cb) {
83+
// Second time fetches user from memory_cache
84+
memory_cache.wrap(key, function (cb) {
8285
get_user(user_id, cb);
8386
}, function (err, user) {
8487
console.log(user);
@@ -95,7 +98,7 @@ node-cache-manager handles easily and transparently.
9598
#### Custom Stores
9699

97100
You can use your own custom store by creating one with the same API as the
98-
build-in redis and memory stores. To use your own store, you can either pass
101+
build-in memory stores (such as a redis or memcached store). To use your own store, you can either pass
99102
in an instance of it, or pass in the path to the module.
100103

101104
E.g.,
@@ -110,7 +113,7 @@ E.g.,
110113
### Multi-Store
111114

112115
```javascript
113-
var multi_cache = cache_manager.multi_caching([memory_cache, redis_cache]);
116+
var multi_cache = cache_manager.multi_caching([memory_cache, some_other_cache]);
114117
user_id2 = 456;
115118
key2 = 'user_' + user_id;
116119

@@ -135,7 +138,7 @@ E.g.,
135138

136139
// Second time fetches user from memory_cache, since it's highest priority.
137140
// If the data expires in the memory cache, the next fetch would pull it from
138-
// the Redis cache, and set the data in memory again.
141+
// the 'some_other_cache', and set the data in memory again.
139142
multi_cache.wrap(key2, function (cb) {
140143
get_user(user_id2, cb);
141144
}, function (err, user) {

examples/example.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
var cache_manager = require('cache-manager');
2-
var redis_cache = cache_manager.caching({store: 'redis', db: 1, ttl: 100/*seconds*/});
1+
var cache_manager = require('../');
32
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
3+
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/*seconds*/});
44

5-
redis_cache.set('foo', 'bar', function(err) {
5+
//
6+
// Basic usage
7+
//
8+
memory_cache2.set('foo', 'bar', function (err) {
69
if (err) { throw err; }
710

8-
redis_cache.get('foo', function(err, result) {
11+
memory_cache2.get('foo', function (err, result) {
912
console.log(result);
1013
// >> 'bar'
11-
redis_cache.del('foo', function(err) {});
14+
memory_cache2.del('foo', function (err) { console.log(err); });
1215
});
1316
});
1417

@@ -20,15 +23,18 @@ function get_user(id, cb) {
2023
}
2124

2225
var user_id = 123;
23-
var key = 'user_' + user_id;
26+
var key = 'user_' + user_id;
2427

25-
redis_cache.wrap(key, function (cb) {
28+
//
29+
// wrap() example
30+
//
31+
memory_cache2.wrap(key, function (cb) {
2632
get_user(user_id, cb);
2733
}, function (err, user) {
2834
console.log(user);
2935

30-
// Second time fetches user from redis_cache
31-
redis_cache.wrap(key, function (cb) {
36+
// Second time fetches user from memory_cache2
37+
memory_cache2.wrap(key, function (cb) {
3238
get_user(user_id, cb);
3339
}, function (err, user) {
3440
console.log(user);
@@ -41,9 +47,9 @@ redis_cache.wrap(key, function (cb) {
4147
// { id: 123, name: 'Bob' }
4248

4349

44-
var multi_cache = cache_manager.multi_caching([memory_cache, redis_cache]);
45-
user_id2 = 456;
46-
key2 = 'user_' + user_id;
50+
var multi_cache = cache_manager.multi_caching([memory_cache, memory_cache2]);
51+
var user_id2 = 456;
52+
var key2 = 'user_' + user_id;
4753

4854
multi_cache.wrap(key2, function (cb) {
4955
get_user(user_id2, cb);
@@ -60,16 +66,17 @@ multi_cache.wrap(key2, function (cb) {
6066
});
6167

6268
// Sets in all caches.
63-
multi_cache.set('foo2', 'bar2', function(err) {
69+
multi_cache.set('foo2', 'bar2', function (err) {
6470
if (err) { throw err; }
6571

6672
// Fetches from highest priority cache that has the key.
67-
multi_cache.get('foo2', function(err, result) {
73+
multi_cache.get('foo2', function (err, result) {
6874
console.log(result);
6975
// >> 'bar2'
7076

7177
// Delete from all caches
72-
multi_cache.del('foo2', function(err) {
78+
multi_cache.del('foo2', function (err) {
79+
console.log(err);
7380
process.exit();
7481
});
7582
});

examples/redis_example/example.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Setup:
2+
// npm install redis
3+
// npm install sol-redis-pool
4+
5+
var cache_manager = require('../../');
6+
var redis_store = require('./redis_store');
7+
var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100/*seconds*/});
8+
9+
redis_cache.set('foo', 'bar', function (err) {
10+
if (err) { throw err; }
11+
12+
redis_cache.get('foo', function (err, result) {
13+
console.log(result);
14+
// >> 'bar'
15+
redis_cache.del('foo', function (err) { console.log(err); });
16+
});
17+
});
18+
19+
function get_user(id, cb) {
20+
setTimeout(function () {
21+
console.log("Returning user from slow database.");
22+
cb(null, {id: id, name: 'Bob'});
23+
}, 100);
24+
}
25+
26+
var user_id = 123;
27+
var key = 'user_' + user_id;
28+
29+
redis_cache.wrap(key, function (cb) {
30+
get_user(user_id, cb);
31+
}, function (err, user) {
32+
console.log(user);
33+
34+
// Second time fetches user from redis_cache
35+
redis_cache.wrap(key, function (cb) {
36+
get_user(user_id, cb);
37+
}, function (err, user) {
38+
console.log(user);
39+
});
40+
});
41+
42+
// Outputs:
43+
// Returning user from slow database.
44+
// { id: 123, name: 'Bob' }
45+
// { id: 123, name: 'Bob' }
46+
47+
process.exit();

examples/redis_example/redis_store.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* This is a very basic example of how you can implement your own Redis-based
3+
* cache store with connection pooling.
4+
*/
5+
6+
var RedisPool = require('sol-redis-pool');
7+
8+
function redis_store(args) {
9+
args = args || {};
10+
var self = {};
11+
var ttl = args.ttl;
12+
self.name = 'redis';
13+
self.client = require('redis').createClient(args.port, args.host, args);
14+
15+
var redis_options = {
16+
redis_host: args.host || '127.0.0.1',
17+
redis_port: args.port || 6379
18+
};
19+
20+
var pool = new RedisPool(redis_options);
21+
22+
function connect(cb) {
23+
pool.acquire(function (err, conn) {
24+
if (err) {
25+
pool.release(conn);
26+
return cb(err);
27+
}
28+
29+
if (args.db || args.db === 0) {
30+
conn.select(args.db);
31+
}
32+
33+
cb(null, conn);
34+
});
35+
}
36+
37+
self.get = function (key, cb) {
38+
connect(function (err, conn) {
39+
if (err) { return cb(err); }
40+
41+
conn.get(key, function (err, result) {
42+
if (err) { pool.release(conn); return cb(err); }
43+
cb(null, JSON.parse(result));
44+
});
45+
});
46+
};
47+
48+
self.set = function (key, value, cb) {
49+
connect(function (err, conn) {
50+
if (err) { return cb(err); }
51+
52+
if (ttl) {
53+
conn.setex(key, ttl, JSON.stringify(value), function (err, result) {
54+
pool.release(conn);
55+
cb(err, result);
56+
});
57+
} else {
58+
conn.set(key, JSON.stringify(value), function (err, result) {
59+
pool.release(conn);
60+
cb(err, result);
61+
});
62+
}
63+
});
64+
};
65+
66+
self.del = function (key, cb) {
67+
connect(function (err, conn) {
68+
if (err) { return cb(err); }
69+
70+
conn.del(key, function (err, result) {
71+
pool.release(conn);
72+
cb(err, result);
73+
});
74+
});
75+
};
76+
77+
return self;
78+
}
79+
80+
var methods = {
81+
create: function (args) {
82+
return redis_store(args);
83+
}
84+
};
85+
86+
module.exports = methods;

lib/stores/redis.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)