_php tips for implementing Message Queuing in PHP Memcache

Source: Internet
Author: User
Tags message queue
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:
Copy Code code 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 Object A");
}
$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->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->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.