PHP memcache Implementation Message Queuing instance

Source: Internet
Author: User
Tags add array object explode implement string strlen thread
Now memcache in the server cache application is more extensive, I will introduce the memcache implementation of Message Queuing waiting for an example, there are need to know friends to refer to.

The principle of Memche Message Queuing is to make a fuss over the key to do a continuous number plus the prefix records after serialization of messages or logs. The content is then landed in a file or database through a timer program.


The use of PHP to implement Message Queuing, such as sending a large number of emails to send a time-consuming problem, you can take the queue.
Lightweight queuing servers for easy implementation of queues are:
Starling lightweight persistent servers that support the Memcache protocol
Https://github.com/starling/starling
BEANSTALKD lightweight, efficient, support for persistence, can handle about 3000 of queues per second
http://kr.github.com/beanstalkd/
You can also use memcache/memcached in PHP to implement Message Queuing.

 -->Connect (' 127.0.0.1 ', 11211);
return $MC;  /** * MC Counter, add count and return a new count * @param string $key counter * @param int $offset count increment, which can be negative. 0 for unchanged count * @param int $time time * @return Int/false failure is return false, count after returning update counter on success/static public function Set_counter ($key, $offset, $time =0) {$MC = sel
F::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) {$m
c = Self::mc_init ();
$w _key = self::P refix. $key. ' W ';
$v _key = self::P refix. $key. Self::set_counter ($w _key, 1);
Return $MC->set ($v _key, $value); /** * Read the data in the queue * @param string $key * @param int $max Read the maximum number of bars * @return Array */static public function outPut ($key, $max =100) {$out = array (); $MC = Self::mc_init (); $r _key = self::P refix. $key. ' r '; $w _key = self::P refix. $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::P refix. $key. $r _p; $r _p = Self::set_counter ($r _key, 1
);
$out [] = $MC->get ($v _key);
$MC->delete ($v _key);
return $out; /** Use method: Qmc::input ($key, $value);//write Queue $list = Qmc::output ($key);//Read queue/?>



Based on PHP shared memory implementation of Message Queuing:

 -->Shmid = Shmop_open ($shmkey, "C", 0644, $this->memsize);
$this->maxqsize = $this->memsize/$this->blocksize;
A signal volume $this->semid = Sem_get ($shmkey, 1); Sem_acquire ($this->semid);
Apply for entry into critical area $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))/$th
is->blocksize; The Public Function EnQueue ($value) {if ($this->ptrinc ($this->rear) = = $this->front) {//Team full return false;} $d
ATA = $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) {//team NULL 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);
Out critical section, release semaphore}}///incoming Operation $SHMQ = new Shmqueue ();
$data = ' test data ';
$SHMQ->enqueue ($data);
Unset ($SHMQ);
Out team Operation $SHMQ = new Shmqueue ();
$data = $shmq->dequeue ();
Unset ($SHMQ); * * * &GT 


For a large message queue, frequent serialization and deserialization of large databases can be costly. Here is a message queue I implemented with PHP, just to insert a data in the tail, the operation of the tail, do not operate the entire message queue for reading, and operation. 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 a few seconds, you can still consider using this.
If you want to implement thread-safe, one suggestion is to lock through the file and then do the operation. Here's the code:
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 E 
Xception ("Memcache object is null, new object"); 
} $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; 
The 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; The 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->max 
Size) {$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-&Gt;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)); 
The 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.