首页 > 其他 > 详细

ruby如何使用redis订阅/发布

时间:2020-07-07 17:50:12      阅读:72      评论:0      收藏:0      [点我收藏+]

redis提供的发布订阅命令
subscribe: subscribe channel [channel …] 订阅一个或多个频道
unsubscribe: unsubscribe [channel [channel …]] 退订频道,如果没有指定频道,则退订所有的频道
publish: publish channel message 给指定的频道发消息
psubscribe: psubscribe pattern [pattern …] 订阅给定模式相匹配的所有频道
punsubscribe: punsubscribe [pattern [pattern …]] 退订给定的模式,如果没有指定模式,则退订所有模式

ruby中订阅一个频道test_channel(长连接)

channel_name = "test_channel"

redis = Redis.current
redis.subscribe(channel_name) do |on|
    on.message do |channel, message|
        Rails.logger.info("listen #{channel}: #{message}")
    end
end

 

然后在另一个console中redis发布

redis = Redis.current
redis.publish(test_channel, hello)

 

ruby如何使用redis订阅/发布

原文:https://www.cnblogs.com/wangyuyu/p/13261730.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!