php - APC缓存不断消失

标签 php apc

似乎每隔 10 分钟或更长时间,缓存就会随机重置一次。我想我用错了。我正在使用 APC 缓存 3 MB HTML 响应。在任何特定时间大约有 50 个。据我了解,使用 mmap_file_mask 将使用文件而不是内存,因此它不会占用我的内存,但我仍然认为它会耗尽内存并自行重置。

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

我应该使用 APC 来执行此操作,还是将响应存储在文件中并直接使用它更好?

我的APC INI如下:

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

编辑:我将内存更新为512 MB,持续了几个小时,然后我就去 sleep 了。当我醒来时,所有缓存都消失了。看来可能是内存问题?我可能只依赖文件或 MySQL,调试 APC 不值得在页面加载上节省几毫秒的时间。

这是所询问的信息:

[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 代码:

<?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
// ...
?>

最佳答案

切勿使用 0 的 ttl。当内存不足时,APC 将刷新所有缓存。

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

如果您没有足够的内存,只需微调 APC 以仅缓存最常访问的文件。

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

关于php - APC缓存不断消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13895226/

相关文章:

php - Apache退出,代码为0 docker apache try

php - PostGisl Pdo 准备插入 : functions quoted

php - 在 IE(和 Firefox)中处理后退按钮的最佳实践是什么

php - 如何在 Linux 上从 C 代码调用 php 脚本

php - APC(备用 PHP 缓存)抛出大量 PHP 通知

javascript - 向 php 脚本提交表单并返回

php - 优雅的 Apache 重启会清除 APC 吗?

php - APC 中的操作码缓存和丢失的文件

php - 为什么我的所有文件都没有缓存 apc 操作码?

php - 多个站点上的 APC 用户缓存 key 冲突