Memcache building a simple Memory message Queue _php Tutorial

Source: Internet
Author: User
This article to the students to introduce the use of memcache to build a simple memory message queue, with a good example to introduce to you, I hope this method for everyone to help OH.

Memcache function is too simple, only set get and delete, can only save Key-value data, cannot save the list. Of course, you can also put a list to serialize the memcache, but there will be a concurrency problem, each time you save data (queue jumping or out of the queue) to the data lock, in high concurrency, it is difficult to ensure the consistency of the data!
But Memcache has a increment operation that adds 1 to the value of a key (which is actually an addition, the default plus 1), which is atomic, so we can use this to maintain a self-increasing ID to keep the data unique. Plus two pointers to maintain the starting key value, so build a simple but phase queue!!

On the code:

The code is as follows Copy Code
/**
* Simple memory queue built by Memcache
*
* @author: jeffjing
*/
Class Memlist {
Private $memcache; Memcache class

Private $queKeyPrefix; Data key Prefix
Private $startKey; 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 list
* Get the start end pointer first, then go 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
* End pointer and move to get a self-increment ID
* Then 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);
}

/**
* Out Team
* Very simple, after the start value is removed, the pointer moves back
*
* @return Mixed
*/
Public function out () {
$result = $this->memcache->get ($this->startkey);
$this->memcache->increment ($this->startkey);
return $result;
}

}

Some things about memcached.


Memory storage mode (slab allocator)

Memcached data storage method is slab allocator that is, data Shard, when the service starts the memory into a different size of chunk, when there is data to be stored in a suitable size of chunk
Previous releases were issues such as allocating memory directly, causing memory fragmentation to be randomly found ...


Data expiration removal mechanism


Memcached after the data expires, does not delete the data, but cannot access the outdated data, the space occupied by the outdated data will be reused
The memcached uses the lazy expiration. Does not proactively scan whether a data item expires, but when the data get is determined whether it has expired.
The deleted algorithm is LRU (Least recently used), with the preference to delete less recently used data


Distributed mechanism of memcached


Although Memcached is a distributed cache, the memcached itself does not implement any distributed mechanism, the distributed function is mainly implemented by the client.
The program adds multiple memcahced services to the client (memcache extension) through Addserver, and before accessing the data, the client first obtains the node that stores the data through the hash algorithm, and then accesses the data. When one of the memcached servers is hung up or a new memcached server is added, the hash algorithm will change the node where the data is stored and go to the new server to access the data.

http://www.bkjia.com/PHPjc/632905.html www.bkjia.com true http://www.bkjia.com/PHPjc/632905.html techarticle This article to the students to introduce the use of memcache to build a simple memory message queue, with a good example to introduce to you, I hope this method for everyone to help OH. Memcache ...

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