标签:def 回调 推送 imu 消息 name process 监视 模拟
<?php //简单字符串缓存 $redis = new \Redis(); $redis->connect(‘127.0.0.1‘,6379); //缓存数据 $redis->set(‘cache_key‘,json_encode([‘data‘=>‘这是缓存数据‘])); //获取缓存数据 $data = $redis->get(‘cache_key‘); print_r(json_decode($data,true));
<?php //利用列表list实现简单队列 $redis = new \Redis(); $redis->connect(‘localhost‘,6379); //进队列 $redis->rpush(‘queue_name‘,json_encode([‘user_id‘=>5])); $redis->rpush(‘queue_name‘,json_encode([‘user_id‘=>6])); $redis->rpush(‘queue_name‘,json_encode([‘user_id‘=>7])); echo "数据进队列完成\n"; //可查看队列 $res = $redis->lrange(‘queue_name‘,0,1000); print_r($res); //出队列 $redis->lpop(‘queue_name‘); //查看队列 $res = $redis->lrange(‘queue_name‘,0,1000); print_r($res);
<?php //实现乐观锁机制 $redis = new \Redis(); $redis->connect(‘localhost‘,6379); //监视count的值 $redis->watch(‘count‘); //开启事务 $redis->multi(); //操作count $redis->set(‘count‘,time()); //-------------------------------- //模拟并发下其他进程对count的操作 //redis-cli 执行 $redis->set(‘count‘,‘is simulate‘); sleep(10); //-------------------------------- //提交事务 $res = $redis->exec(); if($res){ echo ‘sucesss‘; return; }else{ echo ‘fail‘; }
发布publish.php
<?php //发布 $redis = new \Redis(); $redis->connect(‘localhost‘,6379); $redis->publish(‘msg‘,‘来自xxx频道的消息推送‘); echo ‘消息推送成功‘; $redis->close();
订阅subscribe.php
<?php //订阅 //ini_set(‘default_socket_timeout‘,-1); $redis = new \Redis(); $redis->pconnect(‘localhost‘,6379); //订阅 echo "订阅频道等待消息推送\n"; $redis->subscribe([‘msg‘],‘callfun‘); //msg 是频道名 //回调 function callfun($redis,$channel,$msg){ print_r([‘redis‘=>$redis,‘channel‘=>$channel,‘msg‘=>$msg]); }
标签:def 回调 推送 imu 消息 name process 监视 模拟
原文地址:http://www.cnblogs.com/lxwphp/p/7728150.html