I am trying to use redis to store a list of users and weather or not they are online or offline and displaying that information to other users.
我正在尝试使用redis来存储用户和天气列表,不管他们是在线还是离线,并将这些信息显示给其他用户。
I am fairly new to node and I believe that I need to use either a list or sorted sets.
我对节点很新,我相信我需要使用列表或排序集。
when it gets to the console.log(reply); line it only shows "Object"
当它到达console.log(回复);它只显示“对象”
I think I need to loop through the results of the query to build the list but I am not really sure 1) how to loop through the results directly in the server application and 2) how to build the list or sorted set based on that query.
我想我需要遍历查询的结果来构建列表,但我不确定1)如何直接在服务器应用程序中循环结果和2)如何基于该查询构建列表或排序集。
Any advice or suggestions would be greatly appreciated.
任何建议或意见将不胜感激。
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'users'
});
var redis = require('redis')
, client = redis.createClient();
connection.connect();
connection.query('SELECT * FROM user_profile', function(err, rows, fields)
{
if (err) throw err;
client.set('string key', rows[0], redis.print);
client.get("string key", function (err, reply) {
console.log(reply);
});
});
connection.end();
1 个解决方案
#1
2
1) I assume rows contains an array of objects, each object representing a user data record.
1)我假设行包含一个对象数组,每个对象代表一个用户数据记录。
client.set('string key', rows[0], redis.print);
client.set('string key',rows [0],redis.print);
is storing the whole first object of rows
array, you can use a foreach statement to loop over all values returned. You are saving the whole object in redis, but you only need the online/offline state 1
or 0
. Besides, you can store only strings in redis keys (see Redis Keys Docs and Redis Set Docs)
存储行数组的第一个对象,可以使用foreach语句循环返回所有返回的值。您将整个对象保存在redis中,但您只需要在线/离线状态1或0.此外,您只能在redis密钥中存储字符串(请参阅Redis Keys Docs和Redis Set Docs)
2) You don't need a list or sorted sets only for online/offline state of a user, unless you need some sorting operations later. You can use simple keys, I suggest using a pattern like this for key name: "user:".
2)除了稍后需要进行某些排序操作之外,您不需要仅为用户的在线/离线状态设置列表或排序集。您可以使用简单的密钥,我建议使用这样的模式作为密钥名称:“user:”。
// assuming that user_name property exists, holds username data "david" and it's unique
client.set("user:"+row[0].user_name, 0, redis.print); // stores key "user:david" = "0";`
Then to retrieve it use:
然后检索它使用:
client.get("user:"+row[0].user_name);
So, your sql query callback function could look like this:
所以,你的SQL查询回调函数可能如下所示:
function(err, rows, fields) {
if (err) throw err;
rows.forEach(function(element, index, array){
client.set('user:'+element.user_name, 0, redis.print);
client.get("user:"+element.user_name, function (err, reply) {
console.log(reply);
});
});
}
Please note that the user name must be unique. You can use user ID's if not
请注意,用户名必须是唯一的。如果没有,您可以使用用户ID
#1
2
1) I assume rows contains an array of objects, each object representing a user data record.
1)我假设行包含一个对象数组,每个对象代表一个用户数据记录。
client.set('string key', rows[0], redis.print);
client.set('string key',rows [0],redis.print);
is storing the whole first object of rows
array, you can use a foreach statement to loop over all values returned. You are saving the whole object in redis, but you only need the online/offline state 1
or 0
. Besides, you can store only strings in redis keys (see Redis Keys Docs and Redis Set Docs)
存储行数组的第一个对象,可以使用foreach语句循环返回所有返回的值。您将整个对象保存在redis中,但您只需要在线/离线状态1或0.此外,您只能在redis密钥中存储字符串(请参阅Redis Keys Docs和Redis Set Docs)
2) You don't need a list or sorted sets only for online/offline state of a user, unless you need some sorting operations later. You can use simple keys, I suggest using a pattern like this for key name: "user:".
2)除了稍后需要进行某些排序操作之外,您不需要仅为用户的在线/离线状态设置列表或排序集。您可以使用简单的密钥,我建议使用这样的模式作为密钥名称:“user:”。
// assuming that user_name property exists, holds username data "david" and it's unique
client.set("user:"+row[0].user_name, 0, redis.print); // stores key "user:david" = "0";`
Then to retrieve it use:
然后检索它使用:
client.get("user:"+row[0].user_name);
So, your sql query callback function could look like this:
所以,你的SQL查询回调函数可能如下所示:
function(err, rows, fields) {
if (err) throw err;
rows.forEach(function(element, index, array){
client.set('user:'+element.user_name, 0, redis.print);
client.get("user:"+element.user_name, function (err, reply) {
console.log(reply);
});
});
}
Please note that the user name must be unique. You can use user ID's if not
请注意,用户名必须是唯一的。如果没有,您可以使用用户ID