diff --git a/index.js b/index.js index 19db892..c6b8be9 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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); diff --git a/test/koa-redis.test.js b/test/koa-redis.test.js index 362eea9..cf2be7e 100644 --- a/test/koa-redis.test.js +++ b/test/koa-redis.test.js @@ -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'));