php Memcache 不会刷新或清除内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1552597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:03:07  来源:igfitidea点击:

Memcache won't flush or clear memory

phpmemcached

提问by pedalpete

I've been trying to clear my memcache as I'm noticing the storage taking up almost 30% of server memory when using ps -aux.

我一直在尝试清除我的内存缓存,因为我注意到使用ps -aux.

So I ran the following php code.

所以我运行了以下php代码。

$memcache = new Memcache;
    $memcache->connect("localhost",11211);
    $memcache->flush();
    print_r($memcache->getStats());

This results in the output of

这导致输出

(
    [pid] => 4936
    [uptime] => 27318915
    [time] => 1255318611
    [version] => 1.2.2
    [pointer_size] => 64
    [rusage_user] => 9.659531
    [rusage_system] => 49.770433
    [curr_items] => 57864
    [total_items] => 128246
    [bytes] => 1931734247
    [curr_connections] => 1
    [total_connections] => 128488
    [connection_structures] => 17
    [cmd_get] => 170288
    [cmd_set] => 128246
    [get_hits] => 45464
    [get_misses] => 124824
    [evictions] => 1009
    [bytes_read] => 5607431213
    [bytes_written] => 1806543589
    [limit_maxbytes] => 2147483648
    [threads] => 1
)

This should be fairly basic, but clearly, I'm missing something.

这应该是相当基本的,但很明显,我错过了一些东西。

回答by James McNellis

You really need to change the memcached settings so that it doesn't use up as much memory. When you start memcached, you can pass it the amount of memory it should use, in megabytes, using the -mflag. See its documentationfor information.

您确实需要更改 memcached 设置,以免占用太多内存。当您启动 memcached 时,您可以使用-m标志将它应该使用的内存量(以兆字节为单位)传递给它。有关信息,请参阅其文档

flushjust invalidates all of the items in the cache, it doesn't command memcached to deallocate or unreserve the memory it is using. I doubt that you can command memcached to deallocate the memory it is using.

flush只是使缓存中的所有项目无效,它不会命令 memcached 取消分配或取消保留它正在使用的内存。我怀疑您是否可以命令 memcached 释放它正在使用的内存。

回答by Gary Steven

Colin, The Flush All command causes the cache to set all the expiration times to current. The next request for an existing key will return nothing and the record will be removed from the cache. Memcached does not have a separate process to clean expired items and uses a "Lazy" method which makes the process very lightweight and efficient. Because of this if you need to actually remove the cache and start from scratch, the only real way to accomplish this is to restart Memcached. A long workaround would be to dump all your keys, send the Flush All command, then loop through each key running a get against it, causing the record to be removed. Not 100% sure if this method would work but in theory sounds plausible.

Colin,Flush All 命令导致缓存将所有到期时间设置为当前。对现有键的下一个请求将不返回任何内容,并且该记录将从缓存中删除。Memcached 没有单独的进程来清理过期的项目,而是使用“懒惰”的方法,这使得该进程非常轻量和高效。因此,如果您需要实际删除缓存并从头开始,唯一真正的方法是重新启动 Memcached。一个很长的解决方法是转储所有密钥,发送 Flush All 命令,然后循环遍历每个密钥,对其运行 get,从而导致记录被删除。不能 100% 确定这种方法是否有效,但理论上听起来似乎合理。

回答by vkGunasekaran

You need to wait atleast 1 second after clearing memcache. otherwise items added less than one second will invalidate itself.

清除内存缓存后,您至少需要等待 1 秒。否则添加不到一秒的项目将使其自身失效。

Ex:

前任:

$memcache->flush();

$time = time()+1; //one second future
while(time() < $time) {
//sleep
}
$memcache->set('key', 'value'); // repopulate the cache 

Look at this post, memcache flush issue

看这个帖子,memcache flush issue

回答by mulya

Actually, the easiest way to deallocate all values is restart memcached instance.

实际上,释放所有值的最简单方法是重新启动 memcached 实例。

回答by Rom

Try this

尝试这个

Mage::app()->getCacheInstance()->getFrontend()->getBackend()->clean(Zend_Cache::CLEANING_MODE_ALL);

Mage::app()->getCacheInstance()->getFrontend()->getBackend()->clean(Zend_Cache::CLEANING_MODE_ALL);