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

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

Drupal的模塊高級應用之Authcache-動態加載內容教程

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

網站為了提高性能,一般會采用緩存,Drupal中可以實現游客緩存,如果裝上Authcache模塊可以加速用戶登錄響應,對不同的role進行動態加載緩存,以下是教程詳細過程.

本文講一下如果通過修改authcache的核心代碼,來實現緩存頁面的個性化內容,通用的緩存,或多或少都是要進行個性化處理的,比如用戶名顯示、動態加載用戶資料、用戶好友等等.

一般情況下,這種局部個性化,都是通過兩種手段實現:一個是SSI,另一個是CSI.

Authcache本身可以實現局部personalization,模塊叫p13n.

Authcache的ajax模塊屬于CSI,ESI模塊應該是屬于SSI,但是由于ESI模塊需要搭建varnish服務器,配置VCL,加上服務器的設置問題,會導致ESI容易出錯,并且本身ESI傳遞cookie也會有些問題,因此ESI實際上實現起來相當復雜.

所以,如果我們要使用服務器端的personalization,通過PHP修改根據某些條件修改某些內容的話,需要hack一些authcache的代碼.

1.autcache.module文件

找到下面一句,Line 188

// Invoke cache backends and serve page.

修改成如下代碼:

  1. // Invoke cache backends and serve page. 
  2.  if (authcache_page_is_cacheable()) { 
  3.    $cache = authcache_backend_cache_save(); 
  4.    authcache_serve_page_from_cache($cache, authcache_key()); 
  5.  } 
  6.  else { 
  7.    ////process html result 
  8.    global $conf
  9.    $conf['page_compression'] = FALSE; 
  10.  
  11.    $cache = new stdClass(); 
  12.  
  13.    ////process html result 
  14.    $cache->data['body'] = ob_get_contents(); 
  15.    ob_clean(); 
  16.  
  17.    foreach (variable_get('authcache_page_process'array()) as $include) {  //開源軟件:Vevb.com 
  18.      require_once DRUPAL_ROOT . '/' . $include
  19.    } 
  20.    foreach (variable_get('authcache_page_process_interface'array()) as $process) { 
  21.      require_once DRUPAL_ROOT . '/' . $include
  22.      if (is_callable($process)) { 
  23.        $process($cache); 
  24.      } 
  25.    } 
  26.    echo $cache->data['body']; 
  27.  } 
  28.  exit

其中,主要是加了else后面的處理代碼.

2.authcache.cache.inc文件,從85行開始,到函數結尾,修改成如下格式.

  1. $return_compressed = FALSE; ///NEW //Don't send compressed content 
  2.  
  3. if ($page_compression) { 
  4.   header('Vary: Accept-Encoding', FALSE); 
  5.   // If page_compression is enabled, the cache contains gzipped data. 
  6.   if ($return_compressed) { 
  7.     // $cache->data['body'] is already gzip'ed, so make sure 
  8.     // zlib.output_compression does not compress it once more. 
  9.     ini_set('zlib.output_compression''0'); 
  10.     header('Content-Encoding: gzip'); 
  11.   } 
  12.   else { 
  13.     // The client does not support compression, so unzip the data in the 
  14.     // cache. Strip the gzip header and run uncompress. 
  15.     $cache->data['body'] = gzinflate(substr(substr($cache->data['body'], 10), 0, -8)); 
  16.   } 
  17.  
  18. ///NEW 
  19. foreach (variable_get('authcache_page_process'array()) as $include) { 
  20.   require_once DRUPAL_ROOT . '/' . $include
  21. foreach (variable_get('authcache_page_process_interface'array()) as $process) { 
  22.   if (is_callable($process)) { 
  23.     $process($cache); 
  24.   } 

注意:有兩個地方,///NEW 標注,表示新加的內容,中間有一段是原有的code.

改完之后,我們就完工了.

如何使用呢?新建一個文件,比如在custom模塊下面,叫custom_authcache.inc,黏貼如下代碼:

  1. <?php 
  2. /** 
  3. Add the following lines to settings.php 
  4.  
  5. $conf['authcache_page_process'][] = 'sites/all/modules/custom/custom/custom_authcache.inc'; 
  6. $conf['authcache_page_process_interface'][] = 'custom_authcache_common_process'; 
  7.  
  8. If you want to add more process interface, add your function name as an item in this array, $conf['authcache_page_process_interface']. 
  9. If you want to include file, please add file name to this array, $conf['authcache_page_process'] 
  10.  
  11. Core Changes: 
  12. modules/authcache/authcache.cache.inc 
  13. modules/authcache/authcache.module 
  14. **/ 
  15.  
  16. /* 
  17. * Process authcache content to replace content 
  18. */ 
  19. function custom_authcache_common_process(&$cache) { 
  20.   $cache->data['body'] = str_ireplace('<span id="replace_placeholder_1"/>', _get_real_data(), $cache->data['body']); 
  21. ?> 

看上面的注釋,復制兩行代碼到settings.php文件,具體的說明注釋已經很詳細了,相信應該沒問題.

這樣,這個custom_authcache_common_process函數就可以動態替換HTML里面的內容了,達到了個性化頁面的目的.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 綦江县| 望江县| 乐山市| 扶沟县| 华坪县| 禹州市| 濮阳市| 兖州市| 黄冈市| 民丰县| 巴林左旗| 泸溪县| 玉溪市| 策勒县| 裕民县| 子长县| 商城县| 吉首市| 杂多县| 衡阳市| 阿巴嘎旗| 揭西县| 张掖市| 鞍山市| 商洛市| 河西区| 乌拉特后旗| 江华| 二连浩特市| 防城港市| 故城县| 井陉县| 板桥市| 邵阳县| 和顺县| 浦城县| 邛崃市| 普定县| 鹤岗市| 六安市| 广州市|