memcached 中的重复键
我的 php 网站上的 memcache 遇到一些问题。有时我会收到一份报告,指出该站点行为异常,当我查看内存缓存时,我发现集群中的两台服务器上都存在一些密钥。两个条目之间的数据不相同(其中一个较旧)。
我对 memcached 的理解是,这种情况不应该发生......客户端应该散列密钥,然后始终选择相同的服务器。所以要么我的理解是错误的,要么是我的代码是错误的。谁能解释为什么会发生这种情况?
FWIW 服务器托管在 Amazon EC2 上。
我与 memcache 的所有连接都是通过此函数打开的:
$mem_servers = array(
array('ec2-000-000-000-20.compute-1.amazonaws.com', 11211, 50),
array('ec2-000-000-000-21.compute-1.amazonaws.com', 11211, 50)
);
function ConnectMemcache()
{
global $mem_servers;
if ($memcon == 0) {
$memcon = new Memcache();
foreach($mem_servers as $server) $memcon->addServer($server[0], $server[1], true);
}
return($memcon);
}
并且值通过此存储:
function SetData($key,$data)
{
global $mem_global_key;
if(MEMCACHE_ON_OFF)
{
$key = $mem_global_key.$key;
$memcache = ConnectMemcache();
$memcache->set($key, $data);
return true;
}
else
{
return false;
}
}
I'm having some trouble with memcache on my php site. Occasionally I'll get a report that the site is misbehaving and when I look at memcache I find that a few keys exist on both servers in the cluster. The data is not the same between the two entries (one is older).
My understanding of memcached was that this shouldn't happen...the client should hash the key and then always pick the same server. So either my understanding is wrong or my code is. Can anyone explain why this might be happening?
FWIW the servers are hosted on Amazon EC2.
All my connections to memcache are opened through this function:
$mem_servers = array(
array('ec2-000-000-000-20.compute-1.amazonaws.com', 11211, 50),
array('ec2-000-000-000-21.compute-1.amazonaws.com', 11211, 50)
);
function ConnectMemcache()
{
global $mem_servers;
if ($memcon == 0) {
$memcon = new Memcache();
foreach($mem_servers as $server) $memcon->addServer($server[0], $server[1], true);
}
return($memcon);
}
and values are stored through this:
function SetData($key,$data)
{
global $mem_global_key;
if(MEMCACHE_ON_OFF)
{
$key = $mem_global_key.$key;
$memcache = ConnectMemcache();
$memcache->set($key, $data);
return true;
}
else
{
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这篇博文涉及您遇到的问题。
http://www.caiapps.com/duplicate-key-problem- in-memcache-php/
从文章中看来,发生了以下情况:
- 最初具有密钥的内存缓存服务器已丢失
- 使用更新的数据在第二台服务器上重新创建密钥
- 第一个服务器重新上线并使用旧数据进入集群。
- 现在您已将密钥保存在具有不同数据的 2 个服务器上
听起来您可能需要在写入之前使用 Memcache::flush 清除 memcache 集群,以帮助最大限度地减少集群中重复项可能存在的时间。
I think this blog post touches on the problems your having.
http://www.caiapps.com/duplicate-key-problem-in-memcache-php/
From the article it sounds like the following happens:
- a memcache server that has the key originally drops out
- the key is recreated on the 2nd server with updated data
- 1st server come back online and into the cluster with the old data.
- Now you have the keys save on 2 servers with different data
Sounds like you may need to use Memcache::flush to clear out the memcache cluster before your write to help minimize how long duplicates might exist in your cluster.