-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (53 loc) · 1.35 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const Memcached = require('memcached');
const DEFAULT_MAX_AGE = 1000 * 60 * 60 * 24;
module.exports = app => {
app.addSingleton('session-memcached', createMemcached);
}
function createMemcached(config, app) {
const memcached = new Memcached(`${config.host}:${config.port || '11211'}`, {
retries: 10,
retry: 10000,
remove: true,
failOverServers: config.failOverServers || []
});
app.beforeStart(async() => {
app.sessionStore = {
async get(key) {
return new Promise((resolve, reject) => {
memcached.get(key, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
},
async set(key, sess, maxAge) {
return new Promise((resolve, reject) => {
const age = maxAge || DEFAULT_MAX_AGE;
memcached.set(key, sess, maxAge / 1000, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
},
async destroy(key) {
return new Promise((resolve, reject) => {
memcached.del(key, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
},
};
});
return memcached;
}