Authcache模塊和Boost模塊的原理不一樣,Boost模塊是生成靜態頁面,所以緩存的效果最好,速度最快,Authcache模塊是利用Drupal自身的緩存機制,生成頁面緩存,由于進入到了Drupal環節,因此速度沒有Boost緩存快,但是優點就是可以靈活的使用PHP/Drupal相關方法,動態處理數據.
Drupal模塊講解-Authcache緩存原理詳解教程
首先,我們從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;
- } //開源軟件:Vevb.com
- // 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慢一些.
新聞熱點
疑難解答