_php tutorial for implementing Message Queuing in Memcache

Source: Internet
Author: User
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->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;
}
}


http://www.bkjia.com/PHPjc/445008.html www.bkjia.com true http://www.bkjia.com/PHPjc/445008.html techarticle class Memcache_queue {private $memcache; private $name; private $prefix; function __construct ($maxSize, $name, $MEMCA Che, $prefix = __memcache_queue__) {if ($memcache = = null) {...

  • 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.