網站為了提高性能,一般會采用緩存,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.
修改成如下代碼:
- // Invoke cache backends and serve page.
- if (authcache_page_is_cacheable()) {
- $cache = authcache_backend_cache_save();
- authcache_serve_page_from_cache($cache, authcache_key());
- }
- else {
- ////process html result
- global $conf;
- $conf['page_compression'] = FALSE;
- $cache = new stdClass();
- ////process html result
- $cache->data['body'] = ob_get_contents();
- ob_clean();
- foreach (variable_get('authcache_page_process', array()) as $include) { //開源軟件:Vevb.com
- require_once DRUPAL_ROOT . '/' . $include;
- }
- foreach (variable_get('authcache_page_process_interface', array()) as $process) {
- require_once DRUPAL_ROOT . '/' . $include;
- if (is_callable($process)) {
- $process($cache);
- }
- }
- echo $cache->data['body'];
- }
- exit;
其中,主要是加了else后面的處理代碼.
2.authcache.cache.inc文件,從85行開始,到函數結尾,修改成如下格式.
- $return_compressed = FALSE; ///NEW //Don't send compressed content
- if ($page_compression) {
- header('Vary: Accept-Encoding', FALSE);
- // If page_compression is enabled, the cache contains gzipped data.
- if ($return_compressed) {
- // $cache->data['body'] is already gzip'ed, so make sure
- // zlib.output_compression does not compress it once more.
- ini_set('zlib.output_compression', '0');
- header('Content-Encoding: gzip');
- }
- else {
- // The client does not support compression, so unzip the data in the
- // cache. Strip the gzip header and run uncompress.
- $cache->data['body'] = gzinflate(substr(substr($cache->data['body'], 10), 0, -8));
- }
- }
- ///NEW
- foreach (variable_get('authcache_page_process', array()) as $include) {
- require_once DRUPAL_ROOT . '/' . $include;
- }
- foreach (variable_get('authcache_page_process_interface', array()) as $process) {
- if (is_callable($process)) {
- $process($cache);
- }
- }
注意:有兩個地方,///NEW 標注,表示新加的內容,中間有一段是原有的code.
改完之后,我們就完工了.
如何使用呢?新建一個文件,比如在custom模塊下面,叫custom_authcache.inc,黏貼如下代碼:
- <?php
- /**
- Add the following lines to settings.php
- $conf['authcache_page_process'][] = 'sites/all/modules/custom/custom/custom_authcache.inc';
- $conf['authcache_page_process_interface'][] = 'custom_authcache_common_process';
- If you want to add more process interface, add your function name as an item in this array, $conf['authcache_page_process_interface'].
- If you want to include file, please add file name to this array, $conf['authcache_page_process']
- Core Changes:
- modules/authcache/authcache.cache.inc
- modules/authcache/authcache.module
- **/
- /*
- * Process authcache content to replace content
- */
- function custom_authcache_common_process(&$cache) {
- $cache->data['body'] = str_ireplace('<span id="replace_placeholder_1"/>', _get_real_data(), $cache->data['body']);
- }
- ?>
看上面的注釋,復制兩行代碼到settings.php文件,具體的說明注釋已經很詳細了,相信應該沒問題.
這樣,這個custom_authcache_common_process函數就可以動態替換HTML里面的內容了,達到了個性化頁面的目的.
新聞熱點
疑難解答