国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

Drupal模塊講解-Authcache緩存原理詳解教程

2024-08-30 19:06:43
字體:
來源:轉載
供稿:網友

Authcache模塊和Boost模塊的原理不一樣,Boost模塊是生成靜態頁面,所以緩存的效果最好,速度最快,Authcache模塊是利用Drupal自身的緩存機制,生成頁面緩存,由于進入到了Drupal環節,因此速度沒有Boost緩存快,但是優點就是可以靈活的使用PHP/Drupal相關方法,動態處理數據.

Drupal模塊講解-Authcache緩存原理詳解教程

首先,我們從Drupal的bootstrap講起.

  1. function drupal_bootstrap($phase = NULL, $new_phase = TRUE) { 
  2.   // Not drupal_static(), because does not depend on any run-time information. 
  3.   static $phases = array
  4.     DRUPAL_BOOTSTRAP_CONFIGURATION, 
  5.     DRUPAL_BOOTSTRAP_PAGE_CACHE, 
  6.     DRUPAL_BOOTSTRAP_DATABASE, 
  7.     DRUPAL_BOOTSTRAP_VARIABLES, 
  8.     DRUPAL_BOOTSTRAP_SESSION, 
  9.     DRUPAL_BOOTSTRAP_PAGE_HEADER, 
  10.     DRUPAL_BOOTSTRAP_LANGUAGE, 
  11.     DRUPAL_BOOTSTRAP_FULL, 
  12.   ); 
  13. …. 

這是Drupal自帶的bootstrap的幾個環節(Drupal7),從CONFIGURATION、一直到 FULL,這樣整個Drupal就啟動了,所有的模塊也加載了.

其中我們發現,有一個環節叫 PAGE_CACHE,我們來把這個階段的處理函數完整的貼出來,以便大家能更好的理解這段代碼.

  1. function _drupal_bootstrap_page_cache() { 
  2.   global $user
  3.  
  4.   // Allow specifying special cache handlers in settings.php, like 
  5.   // using memcached or files for storing cache information. 
  6.   require_once DRUPAL_ROOT . '/includes/cache.inc'
  7.   foreach (variable_get('cache_backends'array()) as $include) { 
  8.     require_once DRUPAL_ROOT . '/' . $include
  9.   }  //開源軟件:Vevb.com 
  10.   // Check for a cache mode force from settings.php. 
  11.   if (variable_get('page_cache_without_database')) { 
  12.     $cache_enabled = TRUE; 
  13.   } 
  14.   else { 
  15.     drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE); 
  16.     $cache_enabled = variable_get('cache'); 
  17.   } 
  18.   drupal_block_denied(ip_address()); 
  19.   // If there is no session cookie and cache is enabled (or forced), try 
  20.   // to serve a cached page. 
  21.   if (!isset($_COOKIE[session_name()]) && $cache_enabled) { 
  22.     // Make sure there is a user object because its timestamp will be 
  23.     // checked, hook_boot might check for anonymous user etc. 
  24.     $user = drupal_anonymous_user(); 
  25.     // Get the page from the cache. 
  26.     $cache = drupal_page_get_cache(); 
  27.     // If there is a cached page, display it. 
  28.     if (is_object($cache)) { 
  29.       header('X-Drupal-Cache: HIT'); 
  30.       // Restore the metadata cached with the page. 
  31.       $_GET['q'] = $cache->data['path']; 
  32.       drupal_set_title($cache->data['title'], PASS_THROUGH); 
  33.       date_default_timezone_set(drupal_get_user_timezone()); 
  34.       // If the skipping of the bootstrap hooks is not enforced, call 
  35.       // hook_boot. 
  36.       if (variable_get('page_cache_invoke_hooks', TRUE)) { 
  37.         bootstrap_invoke_all('boot'); 
  38.       } 
  39.       drupal_serve_page_from_cache($cache); 
  40.       // If the skipping of the bootstrap hooks is not enforced, call 
  41.       // hook_exit. 
  42.       if (variable_get('page_cache_invoke_hooks', TRUE)) { 
  43.         bootstrap_invoke_all('exit'); 
  44.       } 
  45.       // We are done. 
  46.       exit
  47.     } 
  48.     else { 
  49.       header('X-Drupal-Cache: MISS'); 
  50.     } 
  51.   } 

當我們看到最下面,exit ;(We are done)之處,我們就知道,Drupal已經處理完了請求,后面的環境(Session、數據庫、模塊、FULL)等環節就不用啟動了,因此大大節省了服務器的處理時間和提高了響應時間.

這就是Drupal自帶的緩存處理機制,Drupal自帶的緩存機制缺點也很明顯,就是只對匿名用戶有效.

因此,Authcache模塊就出現了,Authcache就是利用Drupal自帶的緩存機制,實現對登錄用戶的緩存.

繼續看上面的代碼,其中有3行,如下:

  1. foreach (variable_get('cache_backends'array()) as $include) { 
  2.   require_once DRUPAL_ROOT . '/' . $include

其中,獲取’cache_backends’的時候,加載了一個數組變量,所以在Drupal自身的緩存階段要使用到authcache,那就必須修改這個 cache_backends.

果如其然,如下所示,我們在安裝authcache的時候,就必須設置如下變量.

  1. $conf['cache_backends'][] = 'sites/all/modules/authcache/authcache.cache.inc'
  2. $conf['cache_backends'][] = 'sites/all/modules/authcache/modules/authcache_builtin/authcache_builtin.cache.inc'

這個時候,我們就加載進了authcache.cache.inc和文件了.

繼續…我們打開authcache.cache.inc 其中,就是定義一些函數,繼續查看authcache_builtin.cache.inc文件,看到如下代碼:

  1. $delivered = authcache_builtin_cacheinc_retrieve_cache_page(); 
  2. if ($delivered) { 
  3.   exit

也就是說在這個時候,如果命中了緩存就直接輸入頁面內容,不再繼續boot!這個地方也就代替了原本Drupal自己查找緩存和計算命中緩存的邏輯,使用authcache自己的算法,根據用戶的角色不同,使用的緩存不同.

這就是authcache的核心!

當然authcache還可以做更多,比如:

1. 根據用戶不同,生產不同的緩存,需要處理.

2. 配合authcache_p13n模塊,動態處理某些局部頁面,比如某個block.

3. 修改緩存的某個些內容,稍后會詳細講解.

等等,這就是authcache比boost靈活的地方,當然也是缺點,需要調用很多PHP、數據庫等等,肯定比boost慢一些.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达尔| 屏南县| 万宁市| 老河口市| 华坪县| 铜川市| 石棉县| 东丽区| 武平县| 镇坪县| 湟中县| 称多县| 册亨县| 名山县| 池州市| 通化县| 大名县| 新闻| 康定县| 海城市| 镇安县| 望奎县| 星座| 西盟| 厦门市| 中西区| 屏南县| 诏安县| 安顺市| 石泉县| 柳州市| 合江县| 洛阳市| 安仁县| 鹿邑县| 饶河县| 遂平县| 桐柏县| 泽库县| 泰来县| 卓资县|