Skip to content

Commit 677cc4e

Browse files
committed
feat(session): add support for redis session driver
redis driver support for sessions, adonis-redis must be installed Closes adonisjs#190
1 parent 77e2cea commit 677cc4e

6 files changed

Lines changed: 348 additions & 11 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "npm run lint && istanbul cover _mocha --report lcovonly -- -R spec test/unit test/acceptance && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
8-
"coverage": "npm run lint && istanbul cover _mocha test/unit test/acceptance --bail",
8+
"coverage": "npm run lint && istanbul cover _mocha test/unit test/acceptance --bail",
99
"lint": "standard src/**/*.js src/**/**/*.js test/**/*.js providers/*.js"
1010
},
1111
"standard": {
@@ -28,6 +28,7 @@
2828
"license": "MIT",
2929
"devDependencies": {
3030
"adonis-fold": "^3.0.2",
31+
"adonis-redis": "^1.0.0",
3132
"chai": "^3.3.0",
3233
"cheerio": "^0.20.0",
3334
"co-mocha": "^1.1.3",

src/Session/Drivers/Cookie/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ class Cookie {
106106
this.cookieManager.remove(this.request, this.response, this.cookieName)
107107
}
108108

109-
/**
110-
* Cookie values are automatically deleted as per
111-
* their expiry. This method does not do anything
112-
*/
113-
* gc () {}
114-
115109
/**
116110
* called by session class to pass on the request
117111
* and response object.

src/Session/Drivers/Redis/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict'
2+
3+
/**
4+
* adonis-framework
5+
*
6+
* (c) Harminder Virk <virk@adonisjs.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* Redis session driver to store sessions within
14+
* redis.
15+
* @class
16+
* @alias SessionRedisDriver
17+
*/
18+
class Redis {
19+
20+
/**
21+
* Injects ['Adonis/Src/Helpers', 'Adonis/Src/Config']
22+
*/
23+
static get inject () {
24+
return ['Adonis/Src/Helpers', 'Adonis/Src/Config', 'Adonis/Addons/RedisFactory']
25+
}
26+
27+
/**
28+
* @constructor
29+
*/
30+
constructor (Helpers, Config, RedisFactory) {
31+
const redisConfig = Config.get('session.redis')
32+
this.ttl = Config.get('session.age')
33+
this.redis = new RedisFactory(redisConfig, Helpers, false) // do not use cluster for sessions
34+
}
35+
36+
/**
37+
* Reads values for a session id from redis.
38+
*
39+
* @param {String} sessionId
40+
*
41+
* @return {Object}
42+
*/
43+
* read (sessionId) {
44+
try {
45+
const sessionValues = yield this.redis.get(sessionId)
46+
yield this.redis.expire(sessionId, this.ttl) // updating expiry after activity
47+
return JSON.parse(sessionValues)
48+
} catch (e) {
49+
return {}
50+
}
51+
}
52+
53+
/**
54+
* Writes values for a session id to redis.
55+
*
56+
* @param {String} sessionId
57+
* @param {Object} values
58+
*
59+
* @return {Boolean}
60+
*/
61+
* write (sessionId, values) {
62+
const response = yield this.redis.set(sessionId, JSON.stringify(values))
63+
yield this.redis.expire(sessionId, this.ttl)
64+
return !!response
65+
}
66+
67+
/**
68+
* Destorys the record of a given sessionId
69+
*
70+
* @param {String} sessionId
71+
*
72+
* @return {Boolean} [description]
73+
*/
74+
* destroy (sessionId) {
75+
const response = yield this.redis.del(sessionId)
76+
return !!response
77+
}
78+
79+
}
80+
81+
module.exports = Redis

src/Session/Drivers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
module.exports = {
1313
file: require('./File'),
14-
cookie: require('./Cookie')
14+
cookie: require('./Cookie'),
15+
redis: require('./Redis')
1516
}

test/functional/providers.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,16 @@ describe('Providers', function () {
9696
expect(Response.name).to.equal('Response')
9797
})
9898
})
99+
100+
context('Session', function () {
101+
it('should return Session class', function () {
102+
const Config = Ioc.use('Adonis/Src/Config')
103+
const get = sinon.stub(Config, 'get')
104+
get.withArgs('session.driver').returns('cookie')
105+
const sessionManager = Ioc.use('Adonis/Src/Session')
106+
const Session = require('../../src/Session')
107+
expect(sessionManager).deep.equal(Session)
108+
Config.get.restore()
109+
})
110+
})
99111
})

0 commit comments

Comments
 (0)