Example of message queue implementation using PHPmemcache

Source: Internet
Author: User
PHPmemcache implements message queue instances. Currently, memcache is widely used in server cache. here is an example of implementing message queue waiting in memcache. For more information, see. 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. Use of php to implement message queue, for example, PHP memcache to implement message queue instances when sending emails
Currently, memcache is widely used in server cache. here is an example of implementing message queue waiting in memcache. For more information, see.

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.

 Connect ('2017. 0.0.1 ', 11211);} return $ mc;}/*** mc counter, increase the count and return the new count * @ param string $ key counter * @ param int $ offset count increment, which can be negative. 0 indicates that the count is not changed * @ param int $ time * @ return int/false indicates that if the count fails, false is returned, return the counter count after Update */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 reads * @ 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 queue */?>



Message queue based on PHP shared memory:

 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 + $ th Is-> memSize) % ($ this-> memSize)/$ this-> blockSize;} public function enQueue ($ value) {if ($ this-> ptrInc ($ this-> rear) ==$ this-> front) {// return false when the team is full ;} $ 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 (strle N ($ 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); // output critical section, release semaphores}/* // team up $ shmq = new ShmQueue (); $ data = 'Test data'; $ shmq-> enQueue ($ data ); unset ($ shmq); // queue 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:

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.