首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APC缓存一直在消失

APC缓存一直在消失
EN

Stack Overflow用户
提问于 2012-12-16 03:03:49
回答 3查看 1.7K关注 0票数 2

缓存似乎每隔10分钟或更长时间随机重置一次。我想我用错了。我正在使用APC缓存一个3 MegaByte的超文本标记语言的响应。在任何给定的时间,大约有50个。据我所知,使用mmap_file_mask将使用文件而不是内存,所以它不会占用我的内存,但我仍然认为它会耗尽内存并自行重置。

它是一个具有1 GB内存的VPS,通常在运行free时,它会显示512MB到750MB之间的内存使用量(这是我当前的256MB内存大小)。我应该将我的SHM尺寸从256增加到更高的尺寸吗?

我应该使用APC来做这件事,还是更好地将响应存储在文件中并直接使用它?

我的APC INI如下:

代码语言:javascript
复制
extension = apc.so

apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 256
apc.optimization = 0
apc.num_files_hint = 10000
apc.ttl = 0
apc.user_ttl = 0
apc.gc_ttl = 0
apc.cache_by_default = 1
apc.filters = ""
apc.mmap_file_mask = "/tmp/apc.XXXXXX"
apc.slam_defense = 0
apc.file_update_protection = 2
apc.enable_cli = 0
apc.max_file_size = 10M
apc.stat = 1
apc.write_lock = 1
apc.report_autofilter = 0
apc.include_once_override = 0
apc.localcache = 0
apc.localcache.size = 512
apc.coredump_unmap = 0
apc.stat_ctime = 0

编辑:我将内存更新到512MB,持续了几个小时,然后我就上床睡觉了。当我醒来的时候所有的宝藏都不见了。看起来可能是记忆问题?我可能只依赖于文件或MySQL,调试APC不值得在页面加载上节省毫秒的时间。

以下是被询问的信息:

代码语言:javascript
复制
[num_slots] => 8192
[ttl] => 0
[num_hits] => 490
[num_misses] => 390
[num_inserts] => 13663
[expunges] => 0
[start_time] => 1355644615
[mem_size] => 5271328
[num_entries] => 204
[file_upload_progress] => 1
[memory_type] => mmap
[locking_type] => pthread mutex
[cache_list] => Array

然后它只显示缓存的数组,我认为对于调试任何问题都没有什么真正有用的。没有什么比预缓存清除更突出的了。与之前和之后的随机缓存清除器相比,没有太多的变化。不知道这个问题,512 MB的内存应该足以容纳3 MB x 50个条目。即使这样,我也不希望它使用内存。我想让它使用文件。

编辑2:

按照要求,下面是我使用的APC代码:

代码语言:javascript
复制
<?php
$unique_options = "query_options_that_are_unique_to_1_of_the_50_caches";

$refresh_interval = 60*15;
$refresh_triggered = (isset($_REQUEST['refresh']));


// Try to get the APC Cache, if fails, then it was never created
if ($cache_array = apc_fetch(md5($unique_options))) {
    // I got the cache, using its creation_time, see if its refreshable
    $seconds_since_last_refresh = (time() - $cache_array['creation_time']);
    $can_refresh = ($seconds_since_last_refresh >= $refresh_interval);
} else {
    // Apparently this has never had a cache created, so create it
    apc_store(md5($unique_options), array('data'=> get_json($unique_options), 'creation_time'=>time()), 0);
    $cache_array = apc_fetch(md5($unique_options));
    $refresh_triggered = false;
}

// If the cache already existed, and the user is attempting to refresh the cache, and they are allowed to refresh it, then refresh it.
if($refresh_triggered) {
    if($can_refresh) {
        apc_store(md5($unique_options), array('data'=> get_json($unique_options), 'creation_time'=>time()), 0);
        $cache_array = apc_fetch(md5($unique_options));
    } else {
        echo "You can only refresh every 15 minutes<br><br>";
    }
}

$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http' . '://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_parts = parse_url($current_url);
$url_without_query = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];

foreach($_GET as $key => $value) {
    if($key != "refresh")
        $query .= "$key=$value&";
}
echo "Data was last refreshed " . ($seconds_since_last_refresh) . "seconds  ago  (<a href='$url_without_query?{$query}refresh'>Refresh Now</a>)<br><br>";


$data = $cache_array['data'];

// Now process the the information that was cached
// ...
?>
EN

回答 3

Stack Overflow用户

发布于 2012-12-17 22:22:16

切勿使用ttl为0。当APC耗尽内存时,它将刷新所有缓存。

使用apc.php脚本查看内存的使用情况。您应该保留20%的空闲内存(运行数小时后)。

如果你没有足够的内存,只需微调APC,只缓存访问频率最高的文件。

在这里检查我的答案(不是46票以上的那个是错误的) What is causing "Unable to allocate memory for pool" in PHP?

票数 1
EN

Stack Overflow用户

发布于 2013-04-02 17:22:56

我也遇到了同样的问题,我都快疯了!

实际原因是服务器的时间与脚本设置的时间不同。

在命令行上使用以下命令检查服务器上的日期:

代码语言:javascript
复制
date

然后使用以下命令检查PHP时间:

代码语言:javascript
复制
<?php echo date("Y-m-d H:i:s"); ?>

如果存在差异(这就是我所拥有的),那么这就是清除缓存的原因。

检查我的apc.php文件,我现在有超过40分钟的条目,10分钟后它们就会被清除。哇哦!

如果您的日期/时间不同,则可以使用date命令see here设置服务器时间,或者使用以下命令设置PHP时区以与服务器匹配:

代码语言:javascript
复制
date_default_timezone_set('UTC');

您可以在此处找到此函数支持的时区列表:http://www.php.net/manual/en/timezones.php

我希望这对你有帮助,我花了很长时间将我的脚本转换成使用APC,它变得越来越快,现在它们被存储和保存到我指定的时间长度。

票数 1
EN

Stack Overflow用户

发布于 2015-03-10 22:27:44

我今天遇到了同样的问题,在这里找到了解决方案:http://www.itofy.com/linux/cpanel/apc-cache-reset-every-2-hours/

你需要去AccesWHM > Apache Configuration > Piped Log ConfigurationEnable Piped Apache Logs

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13895226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档