This article introduces how to use memcache to build a simple in-memory message queue. We hope this method will be helpful to you.
The memcache function is too simple. It can only be set get and delete. It can only store key-value data and cannot save the list. Of course, you can also save a list to memcache after serialization, but there will be a concurrency problem. You must lock the data every time you save the data (in or out of the queue, it is difficult to ensure data consistency in the case of high concurrency!
However, memcache has an increment operation that adds 1 to the value corresponding to a key (in fact, it is an addition operation. The default value is 1). This operation is atomic, therefore, we can maintain an auto-increment id to ensure that the data is unique. Add two pointers to maintain the starting key value, so that a simple but phase queue is built !!
Code:
The Code is as follows: |
Copy code |
<? Php /** * Simple memory queue built by memcache * * @ Author: jeffjing */ Class memList { Private $ memcache; // memcache class Private $ queKeyPrefix; // data key prefix Private $ startKey; // The start pointer key. Private $ startKey; // end pointer key Public function _ construct ($ key ){ $ This-> queKeyPrefix = "MEMQUE _ {$ key }_"; $ This-> startKey = "MEMQUE_SK _ {$ key }"; $ This-> endKey = "MEMQUE_EK _ {$ key }"; } /** * Get the list * First get the start and end pointer, and then get the data * * @ Return array */ Public function getList (){ $ StartP = $ this-> memcache-> get ($ this-> startKey ); $ EndP = $ this-> memcache-> get ($ this-> endKey ); Empty ($ startP) & $ startP = 0; Empty ($ endP) & $ endP = 0; $ Arr = array (); For ($ I = $ startP; $ I <$ endP; ++ $ I ){ $ Key = $ this-> queKeyPrefix. $ I; $ Arr [] = $ this-> memcache-> get ($ key ); } Return $ arr; } /** * Insert queue * Move the pointer back to get an auto-increment id. * Save the value to the position specified by the pointer. * * @ Return void */ Public function in ($ value ){ $ Index = $ this-> memcache-> increment ($ this-> endKey ); $ Key = $ this-> queKeyPrefix. $ index; $ This-> memcache-> set ($ key, $ value ); } /** * Team-out * It is very simple. After the start value is taken out, the pointer is moved back. * * @ Return mixed */ Public function out (){ $ Result = $ this-> memcache-> get ($ this-> startKey ); $ This-> memcache-> increment ($ this-> startKey ); Return $ result; } } |
About memcached
Memory storage (slab allocator)
Memcached stores data in slab allocator, that is, data fragments. when the service is started, the memory is divided into chunks of different sizes, store the data to a chunk of the proper size.
In earlier versions, memory was allocated directly, leading to random memory fragmentation search and other problems...
Data expiration and deletion Mechanism
Memcached does not delete data after the data expires, but cannot access the expired data. the space occupied by the expired data will be reused.
Memcached uses lazy expiration. Instead of actively scanning whether a data item has expired, memcached uses lazy expiration to determine whether the data item has expired.
The deleted algorithm is LRU (Least Recently Used), which preferentially deletes less Recently Used data.
Distributed Mechanism of memcached
Although memcached is a distributed cache, memcached itself does not implement any distributed mechanism. distributed functions are mainly implemented by clients.
The program adds multiple memcahced services to the client (memcache extension) through addserver. before accessing data, the client obtains the node for storing data through the hash algorithm and then accesses the data, when one of the memcached servers fails or another memcached server is added, the nodes used to store data from the hash algorithm change and access data from the new server.