memcached - PHP using memcache weights and consistent hashing not working

WBOY
Release: 2016-08-04 09:19:24
Original
1540 people have browsed it

The weight of php calling memcahe is invalid

<code>PHP使用Memcached的扩展addServers使用权重,但是结果还是1:1,权重10:0,请问是什么原因呢?  
</code>
Copy after login
Copy after login
<code><?php
/**
 * 测试memcache一致性hash
 * 使用docker运行
 *
 * docker pull memcached
 * docker stop memcache-01
 * docker stop memcache-02
 * docker rm memcache-01
 * docker rm memcache-02
 * docker run --name memcache-01 -p 11201:11211 -d memcached
 * docker run --name memcache-02 -p 11202:11211 -d memcached
 */
$mem = new Memcached;
$mem->addServers(
    [
//        ['192.168.33.4',11201,10],
        ['192.168.33.4',11202,0]
    ]
);
//$mem->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
//$mem->setOption(Memcached::OPT_HASH, Memcached::HASH_MD5);
for($i=0;$i<10000;$i++){
    $key = md5(uniqid($i.'_'));
    $mem->add($key,'1');
}
</code>
Copy after login
Copy after login

Reply content:

The weight of php calling memcahe is invalid

<code>PHP使用Memcached的扩展addServers使用权重,但是结果还是1:1,权重10:0,请问是什么原因呢?  
</code>
Copy after login
Copy after login
<code><?php
/**
 * 测试memcache一致性hash
 * 使用docker运行
 *
 * docker pull memcached
 * docker stop memcache-01
 * docker stop memcache-02
 * docker rm memcache-01
 * docker rm memcache-02
 * docker run --name memcache-01 -p 11201:11211 -d memcached
 * docker run --name memcache-02 -p 11202:11211 -d memcached
 */
$mem = new Memcached;
$mem->addServers(
    [
//        ['192.168.33.4',11201,10],
        ['192.168.33.4',11202,0]
    ]
);
//$mem->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
//$mem->setOption(Memcached::OPT_HASH, Memcached::HASH_MD5);
for($i=0;$i<10000;$i++){
    $key = md5(uniqid($i.'_'));
    $mem->add($key,'1');
}
</code>
Copy after login
Copy after login

  • Conclusion:

  • 1 weight is only effective when using consistent hashing

  • 2 OPT_LIBKETAMA_COMPATIBLE must be used for consistent hashing
    *

  • Question: Two parameters must be effective at the same time, why do they need to be 2?

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template