Redis-based MessageQueue queue Encapsulation

Source: Internet
Author: User

Redis-based MessageQueue queue Encapsulation

 

Redis linked List can be used as a linked List. The high concurrency feature is very suitable for Distributed Parallel message transmission.

 

Left, right, and out

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

The following programs are officially used in the production environment.

Redis-based PHP Message Queue Encapsulation

 '2017. 0.0.1 ', 'Port' => '000000'), $ 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 () ;}/ *** destructor to close the redis link, it is best to disable the active call */public fun Ction _ destruct () {$ this-> close ();}/*** transient connection */private function connect () {$ this-> redis_server = new Redis (); $ this-> redis_server-> connect ($ this-> server, $ this-> port );} /*** persistent connection */public function pconnect () {$ this-> redis_server = new Redis (); $ this-> redis_server-> pconnect ($ this-> server, $ this-> port);}/*** close link */public function close () {$ this-> redis_server-> close ();} /*** insert a message to the queue Information * @ param $ message * @ return mixed */public function put ($ message) {return $ this-> redis_server-> lPush ($ this-> key, $ message);}/*** insert a string of information into the queue * @ 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 );} /*** get a record from the top of the queue * @ re Turn mixed */public function get () {return $ this-> redis_server-> lPop ($ this-> key);}/*** select a database, it can be used to differentiate different queues * @ param $ database */public function select ($ database) {$ this-> redis_server-> select ($ database );} /*** get the queue status, that is, the number of messages in the current queue * @ return mixed */public function size () {return $ this-> redis_server-> lSize ($ this-> key);}/*** get the value of a location, the value of this position * @ param $ pos * @ return mixed */public function is not deleted. View ($ pos) {return $ this-> redis_server-> lGet ($ this-> key, $ pos );} /*** check Redis extension * @ throws Exception */protected function check_environment () {if (! Extension_loaded ('redis ') {throw new Exception ('redis extension not loaded ');}}}

To write data to multiple queues at a time, use the following call method:

 puts(1, 2, 3, 4);$redis->puts(5, 6, 7, 8, 9);

The encapsulation of the output result of HTTPSQS is as follows:

 '2017. 0.0.1 ', 'Port' => '000000'), $ 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 ($ thi S-> 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 s Ize () {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 ());}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.