僅當菜單回調返回輸出時纔會緩存菜單回調的結果;當菜單回調打印輸出時,它不會被緩存。
代碼執行來引導的Drupal當一個頁面被請求如下:
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$return = menu_execute_active_handler();
// Menu status constants are integers; page content is a string.
if (is_int($return)) {
switch ($return) {
case MENU_NOT_FOUND:
drupal_not_found();
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
break;
case MENU_SITE_OFFLINE:
drupal_site_offline();
break;
}
}
elseif (isset($return)) {
// Print any value (including an empty string) except NULL or undefined:
print theme('page', $return);
}
drupal_page_footer();
drupal_page_footer()
是緩存結果的功能。
function drupal_page_footer() {
if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
page_set_cache();
}
module_invoke_all('exit');
}
page_set_cache()
是做實際工作的函數。
function page_set_cache() {
global $user, $base_root;
if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && page_get_cache(TRUE)) {
// This will fail in some cases, see page_get_cache() for the explanation.
if ($data = ob_get_contents()) {
if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
$data = gzencode($data, 9, FORCE_GZIP);
}
ob_end_flush();
cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers());
}
}
}
內容被壓縮(如果啓用了zlib
)並保存在緩存中。
如果您希望緩存自定義菜單回調的輸出,那麼您只需要返回輸出,而不是直接打印輸出。
function mymodule_callback() {
// …
return $output;
}
您可以緩存模塊用於構建其輸出的數據,而不是緩存頁面的輸出。例如,如果使用SQL查詢獲取輸出數據,則可以緩存查詢的結果。
感謝所有的信息kiamlaluno。我返回的是輸出而不是直接打印,所以如果我理解正確,那意味着我的頁面輸出被緩存了 - 當然我打開緩存。對? – 2010-08-03 21:59:39
頁面緩存僅用於匿名用戶(這就是爲什麼'page_set_cache()'檢查'$ user-> uid'),使用GET請求的頁面,以及是否沒有使用'drupal_set_message()'設置消息的原因。 如果頁面包含使用表單API創建的表單,則會使用另一個緩存。 – kiamlaluno 2010-08-04 07:07:36
不管你是否相信kiamlaluno,即使你用過我向你解釋的所有單詞,也要感謝那些順便說一句......我仍然不完全確定我問的答案是否是「是」。對我來說,越來越多的回答肯定是肯定的,但我確定希望你能馬上出來說出是或否。再次感謝。 – 2010-08-04 16:29:11