<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        PHP實現基于Redis的MessageQueue隊列封裝操作示例

        來源:懂視網 責編:小采 時間:2020-11-27 22:01:21
        文檔

        PHP實現基于Redis的MessageQueue隊列封裝操作示例

        PHP實現基于Redis的MessageQueue隊列封裝操作示例:本文實例講述了PHP實現基于Redis的MessageQueue隊列封裝操作。分享給大家供大家參考,具體如下: Redis的鏈表List可以用來做鏈表,高并發的特性非常適合做分布式的并行消息傳遞。 項目地址:https://github.com/huyanping/Zebra-PHP-
        推薦度:
        導讀PHP實現基于Redis的MessageQueue隊列封裝操作示例:本文實例講述了PHP實現基于Redis的MessageQueue隊列封裝操作。分享給大家供大家參考,具體如下: Redis的鏈表List可以用來做鏈表,高并發的特性非常適合做分布式的并行消息傳遞。 項目地址:https://github.com/huyanping/Zebra-PHP-

        本文實例講述了PHP實現基于Redis的MessageQueue隊列封裝操作。分享給大家供大家參考,具體如下:

        Redis的鏈表List可以用來做鏈表,高并發的特性非常適合做分布式的并行消息傳遞。

        項目地址:https://github.com/huyanping/Zebra-PHP-Framework

        左進右出

        $redis->lPush($key, $value);
        $redis->rPop($key);
        
        

        以下程序已在生產環境中正式使用。

        基于Redis的PHP消息隊列封裝

        <?php
        /**
         * Created by PhpStorm.
         * User: huyanping
         * Date: 14-8-19
         * Time: 下午12:10
         *
         * 基于Redis的消息隊列封裝
         */
        namespace Zebra\MessageQueue;
        class RedisMessageQueue implements IMessageQueue
        {
         protected $redis_server;
         protected $server;
         protected $port;
         /**
         * @var 消息隊列標志
         */
         protected $key;
         /**
         * 構造隊列,創建redis鏈接
         * @param $server_config
         * @param $key
         * @param bool $p_connect
         */
         public function __construct($server_config = array('IP' => '127.0.0.1', 'PORT' => '6379'), $key = 'redis_message_queue', $p_connect = false)
         {
         if (empty($key))
         throw new \Exception('message queue key can not be empty');
         $this->server = $server_config['IP'];
         $this->port = $server_config['PORT'];
         $this->key = $key;
         $this->check_environment();
         if ($p_connect) {
         $this->pconnect();
         } else {
         $this->connect();
         }
         }
         /**
         * 析構函數,關閉redis鏈接,使用長連接時,最好主動調用關閉
         */
         public function __destruct()
         {
         $this->close();
         }
         /**
         * 短連接
         */
         private function connect()
         {
         $this->redis_server = new \Redis();
         $this->redis_server->connect($this->server, $this->port);
         }
         /**
         * 長連接
         */
         public function pconnect()
         {
         $this->redis_server = new \Redis();
         $this->redis_server->pconnect($this->server, $this->port);
         }
         /**
         * 關閉鏈接
         */
         public function close()
         {
         $this->redis_server->close();
         }
         /**
         * 向隊列插入一條信息
         * @param $message
         * @return mixed
         */
         public function put($message)
         {
         return $this->redis_server->lPush($this->key, $message);
         }
         /**
         * 向隊列中插入一串信息
         * @param $message
         * @return mixed
         */
         public function puts(){
         $params = func_get_args();
         $message_array = array_merge(array($this->key), $params);
         return call_user_func_array(array($this->redis_server, 'lPush'), $message_array);
         }
         /**
         * 從隊列頂部獲取一條記錄
         * @return mixed
         */
         public function get()
         {
         return $this->redis_server->lPop($this->key);
         }
         /**
         * 選擇數據庫,可以用于區分不同隊列
         * @param $database
         */
         public function select($database)
         {
         $this->redis_server->select($database);
         }
         /**
         * 獲得隊列狀態,即目前隊列中的消息數量
         * @return mixed
         */
         public function size()
         {
         return $this->redis_server->lSize($this->key);
         }
         /**
         * 獲取某一位置的值,不會刪除該位置的值
         * @param $pos
         * @return mixed
         */
         public function view($pos)
         {
         return $this->redis_server->lGet($this->key, $pos);
         }
         /**
         * 檢查Redis擴展
         * @throws Exception
         */
         protected function check_environment()
         {
         if (!\extension_loaded('redis')) {
         throw new \Exception('Redis extension not loaded');
         }
         }
        }
        
        

        如果需要一次寫入多個隊列,可以使用如下調用方式:

        <?php
        $redis = new RedisMessageQueue();
        $redis->puts(1, 2, 3, 4);
        $redis->puts(5, 6, 7, 8, 9);
        
        

        模仿HTTPSQS輸出結果的封裝如下,提供了寫入位置和讀取位置記錄的功能:

        <?php
        /**
         * Created by PhpStorm.
         * User: huyanping
         * Date: 14-9-5
         * Time: 下午2:16
         *
         * 附加了隊列狀態信息的RedisMessageQueue
         */
        namespace Zebra\MessageQueue;
        class RedisMessageQueueStatus extends RedisMessageQueue {
         protected $record_status;
         protected $put_position;
         protected $get_position;
         public function __construct(
         $server_config = array('IP' => '127.0.0.1', 'PORT' => '6379'),
         $key = 'redis_message_queue',
         $p_connect = false,
         $record_status=true
         ){
         parent::__construct($server_config, $key, $p_connect);
         $this->record_status = $record_status;
         $this->put_position = $this->key . '_put_position';
         $this->get_position = $this->key . '_get_position';
         }
         public function get(){
         if($queue = parent::get()){
         $incr_result = $this->redis_server->incr($this->get_position);
         if(!$incr_result) throw new \Exception('can not mark get position,please check the redis server');
         return $queue;
         }else{
         return false;
         }
         }
         public function put($message){
         if(parent::put($message)){
         $incr_result = $this->redis_server->incr($this->put_position);
         if(!$incr_result) throw new \Exception('can not mark put position,please check the redis server');
         return true;
         }else{
         return false;
         }
         }
         public function puts_status(){
         $message_array = func_get_args();
         $result = call_user_func_array(array($this, 'puts'), $message_array);
         if($result){
         $this->redis_server->incrBy($this->put_position, count($message_array));
         return true;
         }
         return false;
         }
         public function size(){
         return $this->redis_server->lSize($this->key);
         }
         public function status(){
         $status['put_position'] = ($put_position = $this->redis_server->get($this->put_position)) ? $put_position : 0;
         $status['get_position'] = ($get_position = $this->redis_server->get($this->get_position)) ? $get_position : 0;
         $status['unread_queue'] = $this->size();
         $status['queue_name'] = $this->key;
         $status['server'] = $this->server;
         $status['port'] = $this->port;
         return $status;
         }
         public function status_normal(){
         $status = $this->status();
         $message = 'Redis Message Queue' . PHP_EOL;
         $message .= '-------------------' . PHP_EOL;
         $message .= 'Message queue name:' . $status['queue_name'] . PHP_EOL;
         $message .= 'Put position of queue:' . $status['put_position'] . PHP_EOL;
         $message .= 'Get position of queue:' . $status['get_position'] . PHP_EOL;
         $message .= 'Number of unread queue:' . $status['unread_queue'] . PHP_EOL;
         return $message;
         }
         public function status_json(){
         return \json_encode($this->status());
         }
        }
        
        

        更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+redis數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

        希望本文所述對大家PHP程序設計有所幫助。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PHP實現基于Redis的MessageQueue隊列封裝操作示例

        PHP實現基于Redis的MessageQueue隊列封裝操作示例:本文實例講述了PHP實現基于Redis的MessageQueue隊列封裝操作。分享給大家供大家參考,具體如下: Redis的鏈表List可以用來做鏈表,高并發的特性非常適合做分布式的并行消息傳遞。 項目地址:https://github.com/huyanping/Zebra-PHP-
        推薦度:
        標簽: php 示例 隊列
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产裸体美女永久免费无遮挡| 国产91色综合久久免费分享| 亚洲AV无码成人网站久久精品大 | 国产人成免费视频| 久久国产精品免费| 亚洲欧洲日产国产最新| 拔擦拔擦8x华人免费久久| 久久免费香蕉视频| 久久精品国产亚洲AV忘忧草18| 亚洲国产精品成人| 亚洲成人免费网址| 免费一级做a爰片久久毛片潮| 亚洲AV无码国产精品色午友在线 | 国产精品亚洲一区二区无码| 亚洲日韩aⅴ在线视频| 成人毛片18女人毛片免费96| 久青草视频在线观看免费| 亚洲av永久无码精品天堂久久| 亚洲日本在线观看视频| 久久受www免费人成_看片中文| 久久久久国色AV免费观看| 色老板亚洲视频免在线观| 亚洲日韩精品A∨片无码| 韩国二级毛片免费播放| 日韩精品无码专区免费播放| 国产亚洲福利一区二区免费看 | 亚洲资源在线视频| 亚洲男女内射在线播放| 国产亚洲人成网站在线观看| 曰批全过程免费视频网址| 国产精品极品美女自在线观看免费| 亚洲一区精品视频在线| 亚洲AV无码1区2区久久| 免费又黄又爽又猛的毛片| 一个人免费高清在线观看| 久久国产精品2020免费m3u8| 免费无码一区二区| 亚洲AV成人片无码网站| 亚洲欧洲精品一区二区三区| 亚洲韩国精品无码一区二区三区| 亚洲AⅤ无码一区二区三区在线|