Memcache consistent hash of PHP implementation _php Tutorial

Source: Internet
Author: User
Tags crc32

PHP implementation method of Memcache consistency hash


This article mainly introduced the Memcache consistency hash PHP implementation method, the example analyzes the hash consistency in the memcache the realization principle and the related skill, needs the friend can refer to the next

This paper describes the PHP implementation method of Memcache consistent hash. Share to everyone for your reference. Specific as follows:

Recently looking at some of the distributed aspects of the article, so just use PHP to achieve consistency hash to practice practiced hand, used to be the most primitive hash to do distributed, when the production process to add or delete a memcache will result in all the data failure, consistency hash is to solve this problem, To minimize the failure data, the relevant information can be Google a bit!

PHP implementation of a certain lack of efficiency, if you want to high efficiency, or write extension is better

After testing, 5 memcache, each memcache generated 100 virtual nodes, set plus get1000 times, and a single memcache direct set plus get 5 times times slower, so the efficiency is general, need to be optimized!

Before reading this article, it is best to know the binary search method.

Implementation process:

Memcache configuration ip+ Port + virtual node serial number to do hash, use is CRC32, form a closed loop.
CRC32 the key to be operated on
Binary method finds the closest virtual node in a virtual node loop
Extract the real memcache IP and port from the virtual node and make a singleton connection

The code is as follows:


Class Memcachehashmap {
Private $_node = Array ();
Private $_nodedata = Array ();
Private $_keynode = 0;
Private $_memcache = null;
Number of virtual nodes per physical server [Note: The more the number of nodes, the better the cache distribution uniformity, while the set get operation, but also more resource-intensive, 10 physical servers, using 200 more reasonable]
Private $_virtualnodenum = 200;
Private Function __construct () {
$config = Array (//five memcache servers
' 127.0.0.1:11211 ',
' 127.0.0.1:11212 ',
' 127.0.0.1:11213 ',
' 127.0.0.1:11214 ',
' 127.0.0.1:11215 '
);
if (! $config) throw new Exception (' Cache config NULL ');
foreach ($config as $key = = $value) {
for ($i = 0; $i < $this->_virtualnodenum; $i + +) {
$this->_node[sprintf ("%u", CRC32 ($value. '_' . $i)] = $value. '_' . $i;//cycle Create 200 virtual nodes for each memcache server
}
}
Ksort ($this->_node);//created 1000 virtual nodes sorted by key name from small to large
}
Instantiate the class
static public Function getinstance () {
static $MEMCACHEOBJ = null;
if (!is_object ($MEMCACHEOBJ)) {
$MEMCACHEOBJ = new self ();
}
return $MEMCACHEOBJ;
}
Find the location of the corresponding virtual node based on the key coming in
Private Function _connectmemcache ($key) {
$this->_nodedata = Array_keys ($this->_node);//array of keys for all virtual nodes
$this->_keynode = sprintf ("%u", CRC32 ($key));//Calculates the hash value of the key
$nodeKey = $this->_findservernode ();//find the corresponding virtual node
If you go beyond the ring, use the dichotomy to find the nearest one, and then the head and tail of the ring to determine the closest node
if ($this->_keynode > End ($this->_nodedata)) {
$this->_keynode-= end ($this->_nodedata);
$nodeKey 2 = $this->_findservernode ();
if (ABS ($nodeKey 2-$this->_keynode) < ABS ($nodeKey-$this->_keynode)) $nodeKey = $nodeKey 2;
}
Var_dump ($this->_node[$nodeKey]);
List ($config, $num) = Explode (' _ ', $this->_node[$nodeKey]);
if (! $config) throw new Exception (' Cache config Error ');
if (!isset ($this->_memcache[$config])) {
$this->_memcache[$config] = new Memcache;
List ($host, $port) = Explode (': ', $config);
$this->_memcache[$config]->connect ($host, $port);
}
return $this->_memcache[$config];
}
Binary method finds the nearest virtual node location based on the given value
Private Function _findservernode ($m = 0, $b = 0) {
$total = count ($this->_nodedata);
if ($total! = 0 && $b = = 0) $b = $total-1;
if ($m < $b) {
$avg = Intval (($m + $b)/2);
if ($this->_nodedata[$avg] = = $this->_keynode) return $this->_nodedata[$avg];
ElseIf ($this->_keynode < $this->_nodedata[$avg] && ($avg-1 >= 0)) return $this->_findservernode ($m, $avg-1);
else return $this->_findservernode ($avg +1, $b);
}
if (ABS ($this->_nodedata[$b]-$this->_keynode) < ABS ($this->_nodedata[$m]-$this->_keynode)) return $ this->_nodedata[$b];
else return $this->_nodedata[$m];
}
Public function set ($key, $value, $expire = 0) {
return $this->_connectmemcache ($key)->set ($key, Json_encode ($value), 0, $expire);
}
Public function Add ($key, $value, $expire = 0) {
return $this->_connectmemcache ($key)->add ($key, Json_encode ($value), 0, $expire);
}
Public function Get ($key) {
Return Json_decode ($this->_connectmemcache ($key)->get ($key), true);
}
Public Function Delete ($key) {
return $this->_connectmemcache ($key)->delete ($key);
}
}
$runData [' begin_time '] = Microtime (true);
Test 10,000 times set plus get
for ($i =0; $i <10000; $i + +) {
$key = MD5 (Mt_rand ());
$b = Memcachehashmap::getinstance ()->set ($key, Time (), 10);
}
Var_dump (Number_format (Microtime (True)-$runData [' Begin_time '],6]);
$runData [' begin_time '] = Microtime (true); $m = new Memcache;
$m->connect (' 127.0.0.1 ', 11211);
for ($i =0; $i <10000; $i + +) {
$key = MD5 (Mt_rand ());
$b = $m->set ($key, Time (), 0, 10);
}
Var_dump (Number_format (Microtime (True)-$runData [' Begin_time '],6]);
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/963976.html www.bkjia.com true http://www.bkjia.com/PHPjc/963976.html techarticle memcache Consistency Hash PHP Implementation method This article mainly introduced the Memcache consistency hash PHP implementation method, the example analysis memcache the hash consistency realization principle and the related skill, needs ...

  • Related Article

    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.