Message Queue in PHP memcache
Last Update:2018-12-08
Source: Internet
Author: User
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. Below is Code :
Copy 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 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;
}
}