Skip to content

Commit

Permalink
Merge pull request #132 from kietsy/master
Browse files Browse the repository at this point in the history
Add option to disable setting a TTL
  • Loading branch information
wavded committed Oct 28, 2014
2 parents 9e06564 + 199a287 commit 1a2f708
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ connect-redis is a Redis session store backed by [node_redis](http://github.com/
The following additional params may be included:

- `ttl` Redis session TTL (expiration) in seconds
- `disableTTL` disables setting TTL, keys will stay in redis until evicted by other means (overides `ttl`)
- `db` Database index to use
- `pass` Password for Redis authentication
- `prefix` Key prefix defaulting to "sess:"
Expand All @@ -37,7 +38,7 @@ Due to express `>= 4` changes, we now need to pass `express-session` to the func
store: new RedisStore(options),
secret: 'keyboard cat'
}));

## FAQ

#### Can I use a URL scheme to make a connection?
Expand Down
44 changes: 27 additions & 17 deletions lib/connect-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ module.exports = function(session){
});
}

this.ttl = options.ttl;
this.ttl = options.ttl;
this.disableTTL = options.disableTTL;

if (options.unref) this.client.unref()

Expand Down Expand Up @@ -138,7 +139,7 @@ module.exports = function(session){
data = data.toString();
debug('GOT %s', data);
try {
result = JSON.parse(data);
result = JSON.parse(data);
} catch (err) {
return fn(err);
}
Expand All @@ -156,24 +157,33 @@ module.exports = function(session){
*/

RedisStore.prototype.set = function(sid, sess, fn){
sid = this.prefix + sid;
try {
var maxAge = sess.cookie.maxAge
, ttl = this.ttl
, sess = JSON.stringify(sess);

ttl = ttl || ('number' == typeof maxAge
? maxAge / 1000 | 0
: oneDay);

debug('SETEX "%s" ttl:%s %s', sid, ttl, sess);
this.client.setex(sid, ttl, sess, function(err){
err || debug('SETEX complete');
fn && fn.apply(this, arguments);
});
sid = this.prefix + sid;
sess = JSON.stringify(sess);

if (this.disableTTL) {
debug('SET "%s" %s', sid, sess);
this.client.set(sid, sess, function(err){
err || debug('SET complete');
fn && fn.apply(this, arguments);
});
} else {
var maxAge = sess.cookie.maxAge;
ttl = this.ttl;

ttl = ttl || ('number' == typeof maxAge
? maxAge / 1000 | 0
: oneDay);

debug('SETEX "%s" ttl:%s %s', sid, ttl, sess);
this.client.setex(sid, ttl, sess, function(err){
err || debug('SETEX complete');
fn && fn.apply(this, arguments);
});
}
} catch (err) {
fn && fn(err);
}
}
};

/**
Expand Down

0 comments on commit 1a2f708

Please sign in to comment.