Message Queue is implemented in phpMemcache. For a large message queue, it is too costly to frequently serialize and deserialize large databases. The following is a message queue I implemented using PHP. I only need to insert a large message queue at the end to frequently serialize and deserialize large databases, which is too costly. 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;
}
}
Serialization and deserialization are too costly. The following is a message queue that I implemented using PHP. you only need to insert it at the end...