Message Queue case sharing using PHP + memcache

Source: Internet
Author: User

The principle of memche message queue is to make an article on the key to record messages or logs after serialization with a continuous number plus a prefix. Then, the contents are stored in files or databases through the Timer Program.

For example, it takes a lot of time to send a large number of emails when sending emails, so you can use a queue.
The lightweight Queue Server that facilitates queue implementation is:
Starling supports the memcache protocol for lightweight persistent servers
Https://github.com/starling/starling
Beanstalkd is lightweight and efficient. It supports persistence and can process about 3000 queues per second.
Http://kr.github.com/beanstalkd/
In php, you can also use memcache/memcached to Implement Message queues.

Copy codeThe Code is as follows:
<? Php
/**
* Memcache Message Queue
*/
Class QMC {
Const PREFIX = 'asdfasdffwqke ';
/**
* Initialize mc
* @ Staticvar string $ mc
* @ Return Memcache
*/
Static private function mc_init (){
Static $ mc = null;
If (is_null ($ mc )){
$ Mc = new Memcache;
$ Mc-> connect ('2017. 0.0.1 ', 127 );
}
Return $ mc;
}
/**
* Mc counters: Increase the count and return a new count.
* @ Param string $ key counter
* @ Param int $ offset count increment, which can be negative. 0 is not changed
* @ Param int $ time
* @ Return int/false: If a counter fails, false is returned. If a counter is successfully updated, the count after the counter is updated is returned.
*/
Static public function set_counter ($ key, $ offset, $ time = 0 ){
$ Mc = self: mc_init ();
$ Val = $ mc-> get ($ key );
If (! Is_numeric ($ val) | $ val <0 ){
$ Ret = $ mc-> set ($ key, 0, $ time );
If (! $ Ret) return false;
$ Val = 0;
}
$ Offset = intval ($ offset );
If ($ offset> 0 ){
Return $ mc-> increment ($ key, $ offset );
} Elseif ($ offset <0 ){
Return $ mc-> decrement ($ key,-$ offset );
}
Return $ val;
}
/**
* Write queue
* @ Param string $ key
* @ Param mixed $ value
* @ Return bool
*/
Static public function input ($ key, $ value ){
$ Mc = self: mc_init ();
$ W_key = self: PREFIX. $ key. 'W ';
$ V_key = self: PREFIX. $ key. self: set_counter ($ w_key, 1 );
Return $ mc-> set ($ v_key, $ value );
}
/**
* Read data from the queue
* @ Param string $ key
* @ Param int $ max: Maximum number of entries read
* @ Return array
*/
Static public function output ($ key, $ max = 100 ){
$ Out = array ();
$ Mc = self: mc_init ();
$ R_key = self: PREFIX. $ key. 'R ';
$ W_key = self: PREFIX. $ key. 'W ';
$ R_p = self: set_counter ($ r_key, 0); // read pointer
$ W_p = self: set_counter ($ w_key, 0); // write pointer
If ($ r_p = 0) $ r_p = 1;
While ($ w_p> = $ r_p ){
If (-- $ max <0) break;
$ V_key = self: PREFIX. $ key. $ r_p;
$ R_p = self: set_counter ($ r_key, 1 );
$ Out [] = $ mc-> get ($ v_key );
$ Mc-> delete ($ v_key );
}
Return $ out;
}
}
/**
Usage:
QMC: input ($ key, $ value); // write queue
$ List = QMC: output ($ key); // read the queue
*/
?>

Message Queue based on PHP shared memory:

Copy codeThe Code is as follows:
<? Php
/**
* Implemented using PHP cyclic memory queue with shared memory
* Supports multi-process storage and various data types
* Note: To release the critical section, use unset () as soon as possible to complete the team-up or team-out operations.
*
* @ Author wangbinandi@gmail.com
* @ Created 2009-12-23
*/
Class ShmQueue
{
Private $ maxQSize = 0; // maximum Queue Length
Private $ front = 0; // head pointer
Private $ rear = 0; // team end pointer
Private $ blockSize = 256; // block size (byte)
Private $ memSize = 25600; // maximum shared memory (byte)
Private $ shmId = 0;
Private $ filePtr = './shmq. ptr ';
Private $ semId = 0;
Public function _ construct ()
{
$ Shmkey = ftok (_ FILE __, 'T ');
$ This-> shmId = shmop_open ($ shmkey, "c", 0644, $ this-> memSize );
$ This-> maxQSize = $ this-> memSize/$ this-> blockSize;
// Apply? A semaphore
$ This-> semId = sem_get ($ shmkey, 1 );
Sem_acquire ($ this-> semId); // apply to enter the critical section
$ This-> init ();
}
Private function init ()
{
If (file_exists ($ this-> filePtr )){
$ Contents = file_get_contents ($ this-> filePtr );
$ Data = explode ('|', $ contents );
If (isset ($ data [0]) & isset ($ data [1]) {
$ This-> front = (int) $ data [0];
$ This-> rear = (int) $ data [1];
}
}
}
Public function getLength ()
{
Return ($ this-> rear-$ this-> front + $ this-> memSize) % ($ this-> memSize)/$ this-> blockSize;
}
Public function enQueue ($ value)
{
If ($ this-> ptrInc ($ this-> rear) ==$ this-> front) {// full team
Return false;
}
$ Data = $ this-> encode ($ value );
Shmop_write ($ this-> shmId, $ data, $ this-> rear );
$ This-> rear = $ this-> ptrInc ($ this-> rear );
Return true;
}
Public function deQueue ()
{
If ($ this-> front ==$ this-> rear) {// empty team
Return false;
}
$ Value = shmop_read ($ this-> shmId, $ this-> front, $ this-> blockSize-1 );
$ This-> front = $ this-> ptrInc ($ this-> front );
Return $ this-> decode ($ value );
}
Private function ptrInc ($ ptr)
{
Return ($ ptr + $ this-> blockSize) % ($ this-> memSize );
}
Private function encode ($ value)
{
$ Data = serialize ($ value). "_ eof ";
Echo '';
Echo strlen ($ data );
Echo '';
Echo $ this-> blockSize-1;
Echo '';
If (strlen ($ data)> $ this-> blockSize-1 ){
Throw new Exception (strlen ($ data). "is overload block size! ");
}
Return $ data;
}
Private function decode ($ value)
{
$ Data = explode ("_ eof", $ value );
Return unserialize ($ data [0]);
}
Public function _ destruct ()
{
$ Data = $ this-> front. '|'. $ this-> rear;
File_put_contents ($ this-> filePtr, $ data );
Sem_release ($ this-> semId); // outputs the critical section and releases the semaphore.
}
}
/*
// Enter the team
$ Shmq = new ShmQueue ();
$ Data = 'test data ';
$ Shmq-> enQueue ($ data );
Unset ($ shmq );
// Team-out operations
$ Shmq = new ShmQueue ();
$ Data = $ shmq-> deQueue ();
Unset ($ shmq );
*/
?>

For a large message queue, it is too costly to frequently serialize and deserialize large databases. The following is a message queue that I implemented using PHP. I only need to insert a data at the end of the queue to operate the end, without reading and operating the entire message queue. However, this message queue is NOT thread-safe. I just try to avoid the possibility of conflict. If the message is not very dense, such as only one message in a few seconds, you can still consider using this method.
If you want to implement thread security, it is recommended to lock the file and then perform operations. The following code is used:
The Code is as follows:

Copy codeThe Code is as follows:
Class Memcache_Queue
{
Private $ memcache;
Private $ name;
Private $ prefix;
Function _ construct ($ maxSize, $ name, $ memcache, $ prefix = "_ memcache_queue __")
{
If ($ memcache = null ){
Throw new Exception ("memcache object is null, new the object first .");
}
$ This-> memcache = $ memcache;
$ This-> name = $ name;
$ This-> prefix = $ prefix;
$ This-> maxSize = $ maxSize;
$ This-> front = 0;
$ This-> real = 0;
$ This-> size = 0;
}
Function _ get ($ name)
{
Return $ this-> get ($ name );
}
Function _ set ($ name, $ value)
{
$ This-> add ($ name, $ value );
Return $ this;
}
Function isEmpty ()
{
Return $ this-> size = 0;
}
Function isFull ()
{
Return $ this-> size = $ this-> maxSize;
}
Function enQueue ($ data)
{
If ($ this-> isFull ()){
Throw new Exception ("Queue is Full ");
}
$ This-> increment ("size ");
$ This-> set ($ this-> real, $ data );
$ This-> set ("real", ($ this-> real + 1) % $ this-> maxSize );
Return $ this;
}
Function deQueue ()
{
If ($ this-> isEmpty ()){
Throw new Exception ("Queue is Empty ");
}
$ This-> decrement ("size ");
$ This-> delete ($ this-> front );
$ This-> set ("front", ($ this-> front + 1) % $ this-> maxSize );
Return $ this;
}
Function getTop ()
{
Return $ this-> get ($ this-> front );
}
Function getAll ()
{
Return $ this-> getPage ();
}
Function getPage ($ offset = 0, $ limit = 0)
{
If ($ this-> isEmpty () | $ this-> size <$ offset ){
Return null;
}
$ Keys [] = $ this-> getKeyByPos ($ this-> front + $ offset) % $ this-> maxSize );
$ Num = 1;
For ($ pos = ($ this-> front + $ offset + 1) % $ this-> maxSize; $ pos! = $ This-> real; $ pos = ($ pos + 1) % $ this-> maxSize)
{
$ Keys [] = $ this-> getKeyByPos ($ pos );
$ Num ++;
If ($ limit> 0 & $ limit = $ num ){
Break;
}
}
Return array_values ($ this-> memcache-> get ($ keys ));
}
Function makeEmpty ()
{
$ Keys = $ this-> getAllKeys ();
Foreach ($ keys as $ value ){
$ This-> delete ($ value );
}
$ This-> delete ("real ");
$ This-> delete ("front ");
$ This-> delete ("size ");
$ This-> delete ("maxSize ");
}
Private function getAllKeys ()
{
If ($ this-> isEmpty ())
{
Return array ();
}
$ Keys [] = $ this-> getKeyByPos ($ this-> front );
For ($ pos = ($ this-> front + 1) % $ this-> maxSize; $ pos! = $ This-> real; $ pos = ($ pos + 1) % $ this-> maxSize)
{
$ Keys [] = $ this-> getKeyByPos ($ pos );
}
Return $ keys;
}
Private function add ($ pos, $ data)
{
$ This-> memcache-> add ($ this-> getKeyByPos ($ pos), $ data );
Return $ this;
}
Private function increment ($ pos)
{
Return $ this-> memcache-> increment ($ this-> getKeyByPos ($ pos ));
}
Private function decrement ($ pos)
{
$ This-> memcache-> decrement ($ this-> getKeyByPos ($ pos ));
}
Private function set ($ pos, $ data)
{
$ This-> memcache-> set ($ this-> getKeyByPos ($ pos), $ data );
Return $ this;
}
Private function get ($ pos)
{
Return $ this-> memcache-> get ($ this-> getKeyByPos ($ pos ));
}
Private function delete ($ pos)
{
Return $ this-> memcache-> delete ($ this-> getKeyByPos ($ pos ));
}
Private function getKeyByPos ($ pos)
{
Return $ this-> prefix. $ this-> name. $ pos;
}
}

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.