gpt4 book ai didi

php - memcached.hash 策略有什么用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:42:47 24 4
gpt4 key购买 nike


我想知道为什么会有 memcache.hash_strategy php.ini 设置。手册说:

Controls which strategy to use when mapping keys to servers. Set this value to consistent to enable consistent hashing which allows servers to be added or removed from the pool without causing keys to be remapped. Setting this value to standard results in the old strategy being used.

但不是程序员自己将 key 映射到服务器吗?这是一些伪代码:


$memcacheServerList = array('host1', 'host2', 'host3');<br/>
$key = 'my_key';<br/>
$memcacheServerIndex = crc32($key) % sizeof($memcacheServerList);



<pre><code>$memcache = new Memcache();
$memcache->connect($memcacheServerList[$memcacheServerIndex], 11211);
$memcache->add($key, 'this is value');
</code></pre>

我错过了什么?

最佳答案

您正在使用一些旧示例。使用内存缓存的现代方式是这样的:

$servers = array(
"10.1.1.1",
"10.1.1.2",
"10.1.1.3",
);

$m = new Memcache();

foreach($servers as $server) {
$m->addServer ( $server );
}

$m->add($key, 'this is value');

现在 memcache 代码将使用它的散列方法来确定服务器。您可以使用两个选项。一致性散列将减少从列表中删除服务器的影响。传统哈希基本上就是您上面的代码。您可以在 http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_-_a_consistent_hashing_algo_for_memcache_clients 找到更多相关信息

关于php - memcached.hash 策略有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5005681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com