Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refix the ttl set #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ RedisStore.prototype.get = function *(sid) {
return null;
}
try {
return JSON.parse(data.toString());
var sess = JSON.parse(data.toString());
//if ttl ,reset it
if(sess.ttl){
this.client.expire(sid,sess.ttl);
}
return sess.sess;
} catch (err) {
// ignore err
debug('parse session error: %s', err.message);
Expand All @@ -130,12 +135,15 @@ RedisStore.prototype.set = function *(sid, sess, ttl) {
if (typeof ttl === 'number') {
ttl = Math.ceil(ttl / 1000);
}
sess = JSON.stringify(sess);
// sess = JSON.stringify(sess);
if (ttl) {
debug('SETEX %s %s %s', sid, ttl, sess);
//save ttl
sess = JSON.stringify({ttl:ttl,sess:sess});
yield this.client.setex(sid, ttl, sess);
} else {
debug('SET %s %s', sid, sess);
sess = JSON.stringify({sess:sess});
yield this.client.set(sid, sess);
}
debug('SET %s complete', sid);
Expand Down
2 changes: 1 addition & 1 deletion test/koa-redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('test/koa-redis.test.js', function () {
client = redisWrapper(client);
yield store.set('key:db1', {a: 2});
(yield store.get('key:db1')).should.eql({a: 2});
JSON.parse(yield client.get('key:db1')).should.eql({a: 2});
JSON.parse(yield client.get('key:db1')).sess.should.eql({a: 2});
yield store.destroy('key:db1');
should.not.exist(yield store.get('key:db1'));
should.not.exist(yield client.get('key:db1'));
Expand Down