相關文章:Drupal的模組進階應用程式之Authcache-動態載入內容教程
Authcache模組和Boost模組的原理不一樣,Boost模組是產生靜態頁面,所以緩衝的效果最好,速度最快。Authcache模組是利用Drupal自身的緩衝機制,產生頁面緩衝,由於進入到了Drupal環節,因此速度沒有Boost緩衝快,但是優點就是可以靈活的使用PHP/Drupal相關方法,動態處理資料。
首先,我們從Drupal的bootstrap講起。
function drupal_bootstrap($phase = NULL, $new_phase = TRUE) {
// Not drupal_static(), because does not depend on any run-time information.
static $phases = array(
DRUPAL_BOOTSTRAP_CONFIGURATION,
DRUPAL_BOOTSTRAP_PAGE_CACHE,
DRUPAL_BOOTSTRAP_DATABASE,
DRUPAL_BOOTSTRAP_VARIABLES,
DRUPAL_BOOTSTRAP_SESSION,
DRUPAL_BOOTSTRAP_PAGE_HEADER,
DRUPAL_BOOTSTRAP_LANGUAGE,
DRUPAL_BOOTSTRAP_FULL,
);
….
}
這是Drupal內建的bootstrap的幾個環節(Drupal7),從CONFIGURATION、一直到 FULL,這樣整個Drupal就啟動了,所有的模組也載入了。
其中我們發現,有一個環節叫 PAGE_CACHE,我們來把這個階段的處理函數完整的貼出來,以便大家能更好的理解這段代碼。
function _drupal_bootstrap_page_cache() {
global $user;
// Allow specifying special cache handlers in settings.php, like
// using memcached or files for storing cache information.
require_once DRUPAL_ROOT . '/includes/cache.inc';
foreach (variable_get('cache_backends', array()) as $include) {
require_once DRUPAL_ROOT . '/' . $include;
}
// Check for a cache mode force from settings.php.
if (variable_get('page_cache_without_database')) {
$cache_enabled = TRUE;
}
else {
drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
$cache_enabled = variable_get('cache');
}
drupal_block_denied(ip_address());
// If there is no session cookie and cache is enabled (or forced), try
// to serve a cached page.
if (!isset($_COOKIE[session_name()]) && $cache_enabled) {
// Make sure there is a user object because its timestamp will be
// checked, hook_boot might check for anonymous user etc.
$user = drupal_anonymous_user();
// Get the page from the cache.
$cache = drupal_page_get_cache();
// If there is a cached page, display it.
if (is_object($cache)) {
header('X-Drupal-Cache: HIT');
// Restore the metadata cached with the page.
$_GET['q'] = $cache->data['path'];
drupal_set_title($cache->data['title'], PASS_THROUGH);
date_default_timezone_set(drupal_get_user_timezone());
// If the skipping of the bootstrap hooks is not enforced, call
// hook_boot.
if (variable_get('page_cache_invoke_hooks', TRUE)) {
bootstrap_invoke_all('boot');
}
drupal_serve_page_from_cache($cache);
// If the skipping of the bootstrap hooks is not enforced, call
// hook_exit.
if (variable_get('page_cache_invoke_hooks', TRUE)) {
bootstrap_invoke_all('exit');
}
// We are done.
exit;
}
else {
header('X-Drupal-Cache: MISS');
}
}
}
當我們看到最下面,exit ;(We are done)之處,我們就知道,Drupal已經處理完了請求,後面的環境(Session、資料庫、模組、FULL)等環節就不用啟動了,因此大大節省了伺服器的處理時間和提高了回應時間。
這就是Drupal內建的緩衝處理機制!!
Drupal內建的緩衝機制缺點也很明顯,就是只對匿名使用者有效。
因此,Authcache模組就出現了,Authcache就是利用Drupal內建的緩衝機制,實現對登入使用者的緩衝。
繼續看上面的代碼,其中有3行,如下:
foreach (variable_get('cache_backends', array()) as $include) {
require_once DRUPAL_ROOT . '/' . $include;
}
其中,擷取’cache_backends’的時候,載入了一個陣列變數,所以在Drupal自身的緩衝階段要使用到authcache,那就必須修改這個 cache_backends。
果如其然,如下所示,我們在安裝authcache的時候,就必須設定如下變數。
$conf['cache_backends'][] = 'sites/all/modules/authcache/authcache.cache.inc';
$conf['cache_backends'][] = 'sites/all/modules/authcache/modules/authcache_builtin/authcache_builtin.cache.inc';
這個時候,我們就載入進了authcache.cache.inc和檔案了。
繼續…
我們開啟authcache.cache.inc 其中,就是定義一些函數。
繼續查看authcache_builtin.cache.inc檔案,看到如下代碼:
$delivered = authcache_builtin_cacheinc_retrieve_cache_page();
if ($delivered) {
exit;
}
也就是說在這個時候,如果命中了緩衝就直接輸入頁面內容,不再繼續boot!這個地方也就代替了原本Drupal自己尋找緩衝和計算命中緩衝的邏輯,使用authcache自己的演算法,根據使用者的角色不同,使用的緩衝不同。
這就是authcache的核心!
當然authcache還可以做更多,比如,
1. 根據使用者不同,生產不同的緩衝(需要處理)。
2. 配合authcache_p13n模組,動態處理某些局部頁面,比如某個block。
3. 修改緩衝的某個些內容。(稍後會詳細講解)
等等,這就是authcache比boost靈活的地方,當然也是缺點,需要調用很多PHP、資料庫等等,肯定比boost慢一些。