2012-12-15 117 views
2

似乎每10分鐘或更長時間,緩存似乎重置。我想我錯了。我正在使用APC來緩存3兆字節的HTML響應。在任何特定時間約有50人。據我瞭解使用mmap_file_mask將使用文件而不是內存,所以它不會佔用我的內存,但我仍然認爲它耗盡內存並重置自身。APC緩存不斷消失

它是一個具有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 

然後它只是顯示緩存,沒有真正用於調試,我認爲任何問題非常有用的陣列。沒有什麼突出預先緩存清除。隨機緩存清除器前後的變化不大。不知道問題,對於3 MB x 50條目,512 MB的內存應該足夠了。即使那樣,我甚至不希望它使用內存。我希望它使用文件。

編輯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

那麼現在呢。緩存是否隨機重置?信息功能說什麼? – hakre

+0

添加了無用的信息功能輸出。 – ParoX

+0

該信息顯示緩存**未被重置**。您的代碼中可能有錯誤,而不是APC中。另外,你已經在輸出中切割了'cache_list'。你應該顯示它,洙。你也應該顯示代碼如何生成信息數據。 – hakre

回答

1

不要使用APC爲0的TTL當它運行的內存將刷新所有緩存。

使用apc.php腳本查看內存的使用情況。您應該保留20%的可用內存(運行幾小時後)。

如果你沒有足夠的內存,只需微調APC緩存最頻繁訪問的文件。

這裏查看我的答案(而不是一個與46票達至極是錯誤的) What is causing "Unable to allocate memory for pool" in PHP?

+0

上作爲模塊運行,'sysctl -a | grep的-E 「SHMALL | SHMMAX」'命令給我: '錯誤: 「不允許操作」 讀取鍵 「net.ipv4.route.flush」 kernel.shmall = 2097152 kernel.shmmax = 33554432' 然而,我想因爲我正在使用'mmap'沒關係?無論如何改變ttl並報告返回 – ParoX

+0

首先使用APC軟件包中的apc.php腳本,它會告訴你APC使用mmap或shm。 – bokan

+0

apc.php很好。我正在使用mmap。我將ttl設置爲1天,6小時後,我設置的緩存消失了。我不認爲這是內存問題,因爲每當我檢查APC它有512 MB我使用了280 MB使用它。我已經在APC中激活了「刪除」範圍,但它對我沒有任何幫助。我仍然對爲什麼感到困惑,但我只是以舊的方式存儲在文件/ mysql中。 – ParoX

1

我有完全一樣的問題,這是推動我瘋了!

實際的原因是服務器時間與腳本設置時間不同。

檢查在命令行上使用此命令的服務器上的日期:

date 

,然後使用檢查你的PHP時間:

<?php echo date("Y-m-d H:i:s"); ?> 

如果是有區別的(這是我有),那麼這就是緩存被清除的原因。

檢查我的apc.php文件我現在有超過40分鐘的條目,在它們在10分鐘後被清除之前。哇噢!

如果您的日期/時間是不同的,那麼你可以設置服務器時使用date命令see here或設置PHP時區使用相匹配的服務器:

date_default_timezone_set('UTC'); 

你可以找到的一個列表此功能在這裏支持時區:http://www.php.net/manual/en/timezones.php

我希望這可以幫助你,我花了年齡轉換我的腳本使用APC,它的閃電快,現在他們正在存儲&保存爲我指定的時間長度。

+0

不幸的是,兩者之間的日期完全相同。 – ParoX