简体   繁体   English

为什么memcache php扩展(或memcached)如此不可靠?

[英]Why is memcache php extension (or memcached) so unreliable?

I am disappointed with memcached. 我对memcached感到失望。 Working with it has been far from easy. 使用它远非易事。

An example: 一个例子:

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");

$memcache->set('id', $array, 120);

I set this about an hour ago - and it is still there! 我将其设置为大约一个小时前-仍然存在! The manual says can use the "number of seconds starting from current time" as parameter. 手册说可以使用“从当前时间开始的秒数”作为参数。 So why is the expiry ignored? 那么为什么到期被忽略?

Another thing that bugs me is that sometimes values are not written. 令我感到困扰的另一件事是,有时没有编写值。 It all is pretty much random. 这几乎都是随机的。 "argyleblanket" mentioned running into those problems in the php manual: http://www.php.net/manual/en/memcache.set.php#84032 I have implemented that fallback on all my replace() calls as well. “ argyleblanket”在php手册中提到了遇到这些问题的方法: http ://www.php.net/manual/zh/memcache.set.php#84032我也已在所有replace()调用上实现了该后备功能。 I don't get why it won't just work on the first call. 我不明白为什么它不能在第一次通话中起作用。 Why offer a replace() function if it's in the stars if it replaces the content or not? 如果替换内容不行,为什么还要提供replace()函数呢?

The question is why would I trust such a software to do anything of importance and is there a way to make it more reliable? 问题是,为什么我会相信这样的软件可以做任何重要的事情,有没有办法使它更可靠?

You're using wrong syntax. 您使用的语法错误。 The 3rd parameter is the compression flag. 第三个参数是压缩标志。

Make a simple interface such as this following. 创建一个简单的界面,如下所示。 It can help you: 它可以帮助您:

/* defines params */
define('MEMCACHED',     1);
define('CACHE_DEFAULT_EXPIRE',  3600);

if(MEMCACHED) if(! class_exists('memcached')) die('memcache not loaded');

/* Cache */
if(MEMCACHED) { 
    global $memcache;
    $memcache = new Memcache();
    $memcache->connect('127.0.0.1', 11211);
}

function cacheSet($key, $var, $expire=NULL) {
    if(!MEMCACHED) return 0;
    global $memcache;
    if(!$expire) $expire = CACHE_DEFAULT_EXPIRE;
    $key = md5($key);
    return $memcache->set($key, $var, false, $expire);
}

function cacheGet($key) {
    if(!MEMCACHED) return 0;
    global $memcache;
    $key = md5($key);
    return $memcache->get($key);
}

The third parameter is Memcache::set is $flag , not $expire . 第三个参数是Memcache::set$ flag ,而不是$ expire $expire is the fourth one: $ expire是第四个:

$memcache = new Memcache;
// add server, etc.
$memcache->set('foo', 'bar', 0, 5); // 5 seconds expiry
var_dump($memcache->get('foo')); // bar
sleep(6);
var_dump($memcache->get('foo')); // false

The syntax you are using is for the Memcached class, not Memcache . 您使用的语法适用于Memcached类,而不适用于Memcache

As for your problem with set/replace, I can't reproduce this with either Memcache or Memcached on PHP 5.3.3. 至于您的设置/替换问题,我无法在PHP 5.3.3上使用Memcache或Memcached重现此问题。

Also, in my opinion, you should go for the PECL memcached extension. 另外,我认为您应该使用PECL memcached扩展。 It provides more features and uses libmemcached directly, so it also should be more efficient. 它提供了更多功能并直接使用libmemcached ,因此它也应该更高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM