redis发布订阅和自定义的命令组合
来源:互联网 发布:大数据资源共享 编辑:程序博客网 时间:2024/06/11 11:25
启动订阅和发布客户端
在订阅客户端
redis 127.0.0.1:6379> PSUBSCRIBE share
Reading messages... (press Ctrl-C to quit)1) "psubscribe"
2) "share"
3) (integer) 1
表示客户端订阅share通道
其中1表示该客户端中连的接订阅通道数为1
在发布客户端,为该通道发布一个消息
redis 127.0.0.1:6379> publish share "share"
(integer) 1
其中1表示有1个连接接收到这个消息
订阅客户端显示:
1) "pmessage"//消息类型
2) "share"//我订阅的通道名
3) "share"//我接收的通道名
4) "share"//消息内容
ps:另附java实现订阅代码:
public static void main(String[] args) {String cmd = "subscribe share\r\n";SocketChannel client = null;try {SocketAddress address = new InetSocketAddress("localhost", 6379);client = SocketChannel.open(address);client.configureBlocking(false);// 设置为异步ByteBuffer buffer = ByteBuffer.allocate(100);buffer.put(cmd.getBytes());buffer.clear();client.write(buffer);System.out.println("发送数据: " + new String(buffer.array()));while (true) {buffer.flip();int i = client.read(buffer);if (i > 0) {byte[] b = buffer.array();System.out.println("接收数据: " + new String(b));break;}}} catch (Exception e) {try {client.close();} catch (IOException e1) {e1.printStackTrace();}e.printStackTrace();}}
2:Redis还支持自定义的命令组合,通过MULTI和EXEC,将几个命令组合起来执行
redis 127.0.0.1:6379> SET counter 0OKredis 127.0.0.1:6379> MULTIOKredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> EXEC1) (integer) 12) (integer) 23) (integer) 3redis 127.0.0.1:6379> GET counter"3"
- redis发布订阅和自定义的命令组合
- Redis 的订阅和发布
- Redis 发布订阅 命令
- Redis 发布订阅命令
- redis发布和订阅
- redis订阅和发布
- Redis的订阅和发布代码
- redis的发布和订阅功能
- redis的发布和订阅功能
- Redis的事务机制和订阅发布
- Redis的发布订阅
- Redis的发布订阅
- Redis的发布订阅
- Redis的发布订阅
- redis的发布订阅
- redis 消息订阅和发布
- Redis-发布和订阅服务
- Redis的订阅发布机制
- 六周任务4.1
- 关于ISE10.1DSP_tool不能用matlabR2008的解决办法
- VC 获取托盘图标信息
- 程设第二轮练习:垂直直方图 解题报告&AC代码
- 第六周实验报告(任务4)
- redis发布订阅和自定义的命令组合
- Learning SQL2
- BASE64编码与解码(Base64Encode,Base64Decode)
- java第二课 java语言基础
- cramfs
- [平衡树]BallIntheBox
- 推荐系统的循序进阶读物(从入门到精通)
- 第6周任务2
- The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1)