MessageQueue queue encapsulation based on Redis

Source: Internet
Author: User
Tags message queue mixed pconnect redis server

Original article, reproduced please indicate the source: http://www.huyanping.cn/?p=275
Author: Jenner

Redis list can be used to do the linked list, high concurrency characteristics are very suitable for distributed parallel message delivery.

Project Address: Https://github.com/huyanping/Zebra-PHP-Framework

Zoojin right out

$redis->lpush ($key, $value);
$redis->rpop ($key);

The following programs are formally used in the production environment.

Redis-based PHP Message Queuing encapsulation

<?php/** * Created by Phpstorm.

* user:huyanping * date:14-8-19 * Time: 12:10 * * Based on Redis Message Queuing Encapsulation * * namespace Zebra\messagequeue;

    Class Redismessagequeue implements Imessagequeue {protected $redis _server;

    protected $server;

    protected $port;

    /** * @var Message Queue sign */protected $key;  /** * construct queues, create Redis links * @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 don't 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, close Redis link, when using long connection, it is best to actively call off */Public Function __destruct () {$this->close ();
        /** * Short Connection/private function connect () {$this->redis_server = new \redis ();
    $this->redis_server->connect ($this->server, $this->port);
        /** * Long connection/Public function pconnect () {$this->redis_server = new \redis ();
    $this->redis_server->pconnect ($this->server, $this->port);
    /** * Closed link/public function close () {$this->redis_server->close ();
        /** * Inserts a message into the queue * @param $message * @return Mixed/Public function put ($message) {
    return $this->redis_server->lpush ($this->key, $message); /** * Inserts a string of information into the queue * @param $message * @return Mixed/Public function puts () {$para
        ms = 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 * @return Mixed */Public function gets () {return $this->redis_
    Server->lpop ($this->key); /** * Select a database that can be used to differentiate between different queue * @param $database/Public Function Select ($database) {$th
    Is->redis_server->select ($database); /** * Gets the queue status, that is, the number of messages in the current queue * @return Mixed/Public function size () {return $this-&G
    T;redis_server->lsize ($this->key);
        /** * Gets the value of a location and does not delete the value of the location * @param $pos * @return Mixed/Public Function 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 '); }
    }
}

If you need to write more than one queue at a time, you can use the following invocation method:

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

The encapsulation of the HTTPSQS output is as follows, providing the ability to write location and read location records:

<?php/** * Created by Phpstorm.

* user:huyanping * date:14-9-5 * Time: 2:16 * * * attached the queue status information 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 _c
        Onfig, $key, $p _connect);
        $this->record_status = $record _status; $this->put_position = $this->key.
        ' _put_position '; $this->get_position = $this->key.
    ' _get_position '; The public function get () {if ($queue = Parent::get ()) {$incr _result = $this->redis_server->in
            CR ($this->get_position); if (! $INCR _result) throw new \exception (' Can not mark get Position,pleaSE Check the Redis server ');
        return $queue;
        }else{return false; The public function put ($message) {if (Parent::p ut ($message)) {$incr _result = $this->redis_
            SERVER-&GT;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;
        The 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;
    The Public function size () {return $this->redis_server->lsize ($this->key); The Public Function status () {$status [' put_position '] = ($put _position = $this->redis_server-&Gt;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;
        The 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; The Public Function Status_json () {return \json_encode($this->status ()); }
}

 

Related Article

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.