Skip to content

Commit 5f08d23

Browse files
committed
fixing redis example
1 parent df97445 commit 5f08d23

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

examples/redis_example/example.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100});
1010
var ttl = 60;
1111

1212
console.log("set/get/del example:");
13-
redis_cache.set('foo', 'bar', ttl, function(err) {
13+
14+
redis_cache.set('foo', 'bar', {ttl: ttl}, function(err) {
1415
if (err) { throw err; }
1516

1617
redis_cache.get('foo', function(err, result) {
@@ -23,6 +24,34 @@ redis_cache.set('foo', 'bar', ttl, function(err) {
2324
});
2425
});
2526

27+
// TTL defaults to what we passed into the caching function (100)
28+
redis_cache.set('foo-no-ttl', 'bar-no-ttl', function(err) {
29+
if (err) { throw err; }
30+
31+
redis_cache.get('foo-no-ttl', function(err, result) {
32+
if (err) { throw err; }
33+
console.log("result fetched from cache: " + result);
34+
// >> 'bar'
35+
redis_cache.del('foo-no-ttl', function(err) {
36+
if (err) { throw err; }
37+
});
38+
});
39+
});
40+
41+
// Calls Redis 'set' instead of 'setex'
42+
redis_cache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) {
43+
if (err) { throw err; }
44+
45+
redis_cache.get('foo-zero-ttl', function(err, result) {
46+
if (err) { throw err; }
47+
console.log("result fetched from cache: " + result);
48+
// >> 'bar'
49+
redis_cache.del('foo-zero-ttl', function(err) {
50+
if (err) { throw err; }
51+
});
52+
});
53+
});
54+
2655
var user_id = 123;
2756

2857
function create_key(id) {
@@ -40,7 +69,7 @@ function get_user_from_cache(id, cb) {
4069
var key = create_key(id);
4170
redis_cache.wrap(key, function(cache_cb) {
4271
get_user(user_id, cache_cb);
43-
}, ttl, cb);
72+
}, {ttl: ttl}, cb);
4473
}
4574

4675
get_user_from_cache(user_id, function(err, user) {

examples/redis_example/redis_store.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ function redis_store(args) {
3434
});
3535
}
3636

37-
self.get = function(key, cb) {
37+
self.get = function(key, options, cb) {
38+
if (typeof options === 'function') {
39+
cb = options;
40+
}
41+
3842
connect(function(err, conn) {
3943
if (err) { return cb(err); }
4044

@@ -46,13 +50,20 @@ function redis_store(args) {
4650
});
4751
};
4852

49-
self.set = function(key, value, ttl, cb) {
50-
var ttlToUse = ttl || ttlDefault;
53+
self.set = function(key, value, options, cb) {
54+
if (typeof options === 'function') {
55+
cb = options;
56+
options = {};
57+
}
58+
options = options || {};
59+
60+
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : ttlDefault;
61+
5162
connect(function(err, conn) {
5263
if (err) { return cb(err); }
5364

54-
if (ttlToUse) {
55-
conn.setex(key, ttlToUse, JSON.stringify(value), function(err, result) {
65+
if (ttl) {
66+
conn.setex(key, ttl, JSON.stringify(value), function(err, result) {
5667
pool.release(conn);
5768
cb(err, result);
5869
});

0 commit comments

Comments
 (0)