網(wǎng)站為了提高性能,一般會(huì)采用緩存,Drupal中可以實(shí)現(xiàn)游客緩存,如果裝上Authcache模塊可以加速用戶登錄響應(yīng),對不同的role進(jìn)行動(dòng)態(tài)加載緩存,以下是教程詳細(xì)過程.
本文講一下如果通過修改authcache的核心代碼,來實(shí)現(xiàn)緩存頁面的個(gè)性化內(nèi)容,通用的緩存,或多或少都是要進(jìn)行個(gè)性化處理的,比如用戶名顯示、動(dòng)態(tài)加載用戶資料、用戶好友等等.
一般情況下,這種局部個(gè)性化,都是通過兩種手段實(shí)現(xiàn):一個(gè)是SSI,另一個(gè)是CSI.
Authcache本身可以實(shí)現(xiàn)局部personalization,模塊叫p13n.
Authcache的ajax模塊屬于CSI,ESI模塊應(yīng)該是屬于SSI,但是由于ESI模塊需要搭建varnish服務(wù)器,配置VCL,加上服務(wù)器的設(shè)置問題,會(huì)導(dǎo)致ESI容易出錯(cuò),并且本身ESI傳遞cookie也會(huì)有些問題,因此ESI實(shí)際上實(shí)現(xiàn)起來相當(dāng)復(fù)雜.
所以,如果我們要使用服務(wù)器端的personalization,通過PHP修改根據(jù)某些條件修改某些內(nèi)容的話,需要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行開始,到函數(shù)結(jié)尾,修改成如下格式.
- $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);
- }
- }
注意:有兩個(gè)地方,///NEW 標(biāo)注,表示新加的內(nèi)容,中間有一段是原有的code.
改完之后,我們就完工了.
如何使用呢?新建一個(gè)文件,比如在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']);
- }
- ?>
看上面的注釋,復(fù)制兩行代碼到settings.php文件,具體的說明注釋已經(jīng)很詳細(xì)了,相信應(yīng)該沒問題.
這樣,這個(gè)custom_authcache_common_process函數(shù)就可以動(dòng)態(tài)替換HTML里面的內(nèi)容了,達(dá)到了個(gè)性化頁面的目的.
新聞熱點(diǎn)
疑難解答