我有以下nodejs代码
const redis = require('redis');
const client = redis.createClient();
module.exports = client;
在文件中,以下代码在另一个
中redis.set(user.id, JSON.stringify(user), function (error) {
if (error) {
logger.log('error', "Redis error: " + error)
}
console.log("Saved to Redis");
目的是在反序列化redis的用户时从redis读取值
redis.get(id, function (error, data) {
if (error) {
logger.log('error', 'Redis error');
}
if (data) {
console.log("Redis");
let redisUser = JSON.parse(data);
done(null, redisUser);
} else {
console.log("Mongo");
userDb.findById(id)
.then(user => done(null, user))
.catch(err => logger.log('error', 'Error when deserializing the user: ' + err))
}
})
但是,当使用Chrome瀑布计时时,我发现Redis所花费的时间与MongoDb所花费的时间几乎相同(大约为180-270毫秒),但是我将redis实施为一个更快的存储库,以便更快地进行检索。我不知道错误可能是什么,但是我怀疑这可能是我的其他代码,用于Socket.IO使用pubClient和SubClient将会话保存到Redis。该代码位于另一个文件中,如下所示
'use strict';
const config = require('./config')
const redis = require('redis').createClient;
const adapter = require('socket.io-redis');
//Social authentication Logic
require('./auth')();
let ioServer = app => {
app.locals.chatrooms = [];
const server = require('http').Server(app);
const io = require('socket.io')(server);
/** Force socket Io to use web sockets as its default transportation medium
*Implements session affinity on the server
*/
io.set('transports', ['websocket']);
//Interface for sending or publishing data buffers
let pubClient = redis(config.redis.port, config.redis.host, {
auth_pass: config.redis.password
});
/**Interface for subscribing or getting data
*Return buffers is to tell redis to return data in its original data format,
*By default redis will return data as Strings
*/
let subClient = redis(config.redis.port, config.redis.host, {
return_buffers: true,
auth_pass: config.redis.password
});
//Specify a custom adapter to Socket.io
io.adapter(adapter({
pubClient,
subClient
}));
io.use((socket, next) => {
require('./session')(socket.request, {}, next);
})
require('./socket')(io, app);
return server;
}
module.exports = {
router: require('./routes')(),
session: require('./session'),
ioServer,
logger:require('./logger')
}
可能是什么原因,我可能是对的,这是错误,如果是,那么将redis集成到socket.io和缓存的可能方法是什么。预先感谢。