强制memcached写入池中的所有服务器

Ind*_*ial 4 php memcached reliability

我已经考虑过如何确保将特定密钥分发给池中的所有memcached服务器.

我当前未经测试的解决方案是创建另一个memcached实例,如下所示:

$cluster[] = array('host' => '192.168.1.1', 'port' => '11211', 'weight' => 50);

$this->tempMemcached = new Memcached;
$this->tempMemcached->addServers($cluster);

foreach ($this->cluster() as $cluster) {    

    $this->tempMemcached->setByKey($cluster, $key, $value, $this->compress, $expireTime);

}

$this->tempMemcache->close();
Run Code Online (Sandbox Code Playgroud)

在这种情况下,当某些密钥需要存储在所有服务器上以确保可靠性时,常识是什么?

min*_*das 6

我认为你没有按照它的设计方式使用memcached.请查看常见问题解答:您应该只存储项目的单个副本,根据所使用的散列算法,它会将您的项目放在某个节点上.

现在,如果您想在所有节点中都有一个项目,那么唯一的方法就是迭代,就像您现在所做的那样.

我希望您的代码能够处理在缓存中找不到此项的情况.

  • @Industrial:这是你最好的.正如其他人所说,这不是memcached的意图,因此创建者实现某种复制机制是没有意义的.迭代是唯一的方法. (2认同)