我使用yar
作为我的会话插件以及catbox-redis
。
这是我的配置:
const Hapi = require('hapi');
const yar = require('yar');
const hapiServer = new Hapi.Server({
cache: {
engine: require('catbox-redis'),
host: redisAddress,
port: redisPort
}
});
hapiServer.connection({
host: hapiHostAddress,
port: hapiPort
});
hapiServer.route({
method: 'GET',
path: '/test/{key}/{value}',
handler: function (request, reply) {
// In MongoDB, this data will be stored in the
// "!yar" collection within the "hapi-cache" database.
var data = {};
data[request.params.key] = request.params.value;
request.yar.set('example', data);
return reply(request.yar.get('example'));
}
});
const yarOptions = {
storeBlank: false,
maxCookieSize: 0,
cache: {
expiresIn: 24 * 60 * 60 * 1000
},
cookieOptions: {
password: 'the-password-must-be-at-least-32-characters-long',
isSecure: true
}
};
hapiServer.register({
register: yar,
options: yarOptions
}, function (err) {
// start your hapiServer after plugin registration
hapiServer.start(function (err) {
console.log('info', 'Server running at: ' + hapiServer.info.uri)
})
})
问题是request.yar.get('example')
会返回正确的数据,但它不会使用redis(我用redis-cli monitor
监视redis),即使request.yar.set('example', data)
将数据保存到redis。< / p>