简体   繁体   English

Node.js redis 发布订阅

[英]Node.js redis pubsub

I'm really beginner of nodejs.我真的是 nodejs 的初学者。 I want to make a chatting service using nodejs.我想用 nodejs 做一个聊天服务。 I use nodejs/jade/mysql to construct basic part of my system and now I want to provide pub/sub to users.我使用 nodejs/jade/mysql 来构建我的系统的基本部分,现在我想向用户提供 pub/sub。

We receive users' interests from text field or using hash tags (anyway we received users' interests and stored in MySQL -> we did it).我们从文本字段或使用哈希标签接收用户的兴趣(无论如何我们收到用户的兴趣并存储在 MySQL 中 -> 我们做到了)。 Then, we want to show users chatting room list according to their interests.然后,我们要根据用户的兴趣显示用户聊天室列表。 For instance A's interests are 'game', 'car' and 'food', then we search chat rooms with 'game', 'car', 'food' and show A these chat rooms first.例如A的兴趣是“游戏”、“汽车”和“食物”,然后我们用“游戏”、“汽车”、“食物”搜索聊天室,并首先向A显示这些聊天室。

I want to use redis to provide this service but i really have no idea!我想用 redis 来提供这个服务,但我真的不知道!

1) I installed redis and can run redis-server. 1)我安装了redis并且可以运行redis-server。
2) 2)

 //redis var redis = require('redis'); var publisher = redis.createClient(); var subscriber = redis.createClient(); subscriber.on('message', function(channel, message){ console.log('Message ' + message + ' on channel ' + channel + ' arrived!'); }); subscriber.on('subscribe', function(channel){ publisher.publish('test', 'the a team'); publisher.publish('test', 'the b team'); }) subscriber.subscribe('test');

This is short code that I tried to understand redis.这是我试图理解 redis 的短代码。

3) I don't know how can I read data stored in Mysql and show users chat room according to their interests using redis. 3)我不知道如何使用redis读取存储在Mysql中的数据并根据用户的兴趣向用户显示聊天室。

Redis is a advanced key-value cache and store.Its operations cannot be directly mapped to mysql. Redis 是一个高级的key-value 缓存和存储,它的操作不能直接映射到mysql。

In redis you can set either key value pair or a hash under a key.在 redis 中,您可以在键下设置键值对或散列。 That is : If you want to store your name in redis it can be done by:也就是说:如果您想将您的名字存储在 redis 中,可以通过以下方式完成:

 var client = redis.createClient(); client.set("name", "John")

Retrieve the values using client.get("name")使用 client.get("name") 检索值

Similarly under a single key you can store multiple key value pairs, as hash.同样,在单个键下,您可以将多个键值对存储为哈希。 That under a name if you want to store their details like age, place, company etc.Then hash should be used.如果您想存储他们的详细信息(例如年龄、地点、公司等),则使用名称命名。然后应使用哈希。 Redis has method "hmset" and "hmget" for hash opertaions. Redis 具有用于哈希操作的方法“hmset”“hmget”

In redis like in cache you can set expiry time.在像缓存一样的 redis 中,您可以设置到期时间。

There are different method available.有不同的方法可用。 You can explore those.你可以探索这些。 For reference http://redis.io/commands供参考http://redis.io/commands

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM