利用Redis模拟简单队列工作
来源:互联网 发布:装饰公司预算软件 编辑:程序博客网 时间:2024/05/21 19:24
【原创】利用Redis模拟简单队列工作
写得有点乱了,勿喷
/** * @name RedisQueue * @uses simulation queue work * @author jiang kejun <jinhua_k9@163.com> * @since 2013.08.27 * @version $Id: code.php 200 2013.08.27 create jkj $ * @example : * # if(php_sapi_name()=='cli'){ #$rq1 = new RedisQueueServer("w1"); #$rq1->openService(); # } */ class RedisQueue{var $_redis = null;var $_key = null;var $_keyno = null;var $retmsg = null;// various statesconst WAITING = 2;const FINISHED = 3;const ACCEPTING = 4;const STOPING = 5;const CALLING = 6;function __construct(){try{$this->_redis = new Redis();$this->_redis->connect('127.0.0.1', 6379, 10);}catch(RedisException $r){printf("The redis-server is not open or unable to connect [%s:%s]! line:[%s]", '127.0.0.1', 6379, __LINE__ );exit();}$this->_key = date("ymd");$this->_keyno = $this->_key."_no";if(!$this->_redis->exists($this->_keyno)){// base number$this->_redis->incrBy($this->_keyno, 100);}}/** * push a queue * @access public * * @return bool */function pushQueue(){// push$nr = $this->_redis->incr($this->_keyno);$this->msgHelper(self::WAITING, $nr);return $this->_redis->rPush($this->_key, $nr);}/** * pop the current queue * @access public * * @return void */function popQueue(){$this->_redis->lPop($this->_key);}/** * queue length * * @return int */function length(){return $this->_redis->lSize($this->_key);}/** * return message helper (log) * * @param int $type * @param mixed $nr * @return void */function msgHelper($type, $nr){switch($type){case self::WAITING:$array = array('error'=>0,'message'=>'no:'.$nr.';There are '.$this->length().' line up before!');$this->retmsg = json_encode($array);break;case self::STOPING:$array = array('error'=>1,'message'=>'stop all');$this->retmsg = json_encode($array);break;case self::CALLING:$array = array('error'=>0,'message'=>'Call transferred to deal with state after 5 seconds');$this->retmsg = json_encode($array);break;case self::ACCEPTING:$array = array('error'=>0,'message'=>'Is being dealt with business');$this->retmsg = json_encode($array);break;default:$array = array('error'=>2,'message'=>'unknown');$this->retmsg = json_encode($array);break;}}/** * flush all db (cache) * * @return void */function flush(){echo "clearing..\n";$this->_redis->flushAll();echo "ok\n";}function getQueueInfo(){}/** * destroy * @return void */function __destruct(){$this->_redis->close();}}class RedisQueueServer extends RedisQueue{var $_current = null;var $_service = null;// processing stateconst FINISHED = 1;const SKIP = 2;const WORKING = 0;// initfunction __construct($windowno){parent::__construct();$this->_service = $windowno;echo "Window is open service...\n";}/** * open service * * @return bool */function openService(){if(!$this->_redis->exists($this->_service)){$this->_redis->hSet($this->_service, 'status', 1);}$i=0;while($this->_current = $this->_redis->lGet($this->_key, 0 )){$this->_redis->hSet($this->_service, $this->_current, self::WORKING);$this->msgHelper(parent::CALLING, $this->_current);echo "please ".$this->_current." to ".$this->_service." window transaction\n";echo "input cmd:";$cmd = trim(fgets(STDIN));if($cmd == "1"){echo $this->_current." is working\n";$this->msgHelper(parent::ACCEPTING, $this->_current);// skip}elseif($cmd == "0"){$this->popQueue();$this->_redis->hSet($this->_service, $this->_current, self::SKIP);$i++;continue;}$cmd = trim(fgets(STDIN));// finishedif($cmd == 'y'){$this->popQueue();$this->_redis->hSet($this->_service, $this->_current, self::FINISHED);$this->msgHelper(parent::FINISHED, $this->_current);$i++;}elseif($cmd=='n'){// quit}elseif($cmd=='q'){echo "Is out of the queue...\n";exit();}sleep(2);}if($i){echo "ok,You dealt with $i m of business success";}else{echo "is an empty queue";}//return false;}/** * stop the current service * return void */function stop(){$this->msgHelper(parent::STOPING, 0);$this->_redis->hSet($this->_service, 'status', 2);}}class RedisQueueClient extends RedisQueue{}
- 利用Redis模拟简单队列工作
- redis队列简单模拟
- redis实现简单队列
- 利用两个栈模拟队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- POJ 3032 简单队列模拟
- 简单数据结构之队列模拟
- 简单模拟牌号系统 <队列>
- PTA-银行业务队列简单模拟
- Redis实现简单消息队列
- Redis实现简单消息队列
- redis 队列的简单实现
- redis简单的消息队列
- asp.net 发送邮件(已知邮箱向另一个未知邮箱发送)
- ajax原理
- ESB知识点介绍
- 学习 Mercurial与TortoiseHg使用入门教程
- STL中list,vector,deque,map,set区别、联系和使用场景
- 利用Redis模拟简单队列工作
- AppWidget应用(二)---PendingIntent 之 getActivity
- select、poll、epoll的比较
- SQLServer 2000自定义函数中用getdate()报错
- php curl会话
- ASPOSE Excel java 单元格的下拉列表 验证
- 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)
- 如何将eclipse 的android工程 签名打包 然后安装到终端上。
- chromium UI库简介