缓存似乎每 10 分钟或更长时间随机重置一次。我想我用错了。我正在使用 APC 缓存 3 兆字节的 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,持续了几个小时,然后我就去睡觉了。当我醒来时,所有的缓存都不见了。好像是内存问题?我可能只依赖文件或 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
// ...
?>