聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

Authcache模块高级篇-动态加载内容

2014-06-14 03:25 浏览: 1713827 次 我要评论(0 条) 字号:

缓存可以提高网站的访问速度,drupal中Boost实现匿名用户的缓存,Authcache可以加速登录用户的响应时间,对不同的role进行缓存。
本文就authcahe的核心高级使用,做一个简单介绍,之前有几篇关于authcache的介绍,参考下面几篇:
Drupal缓存 – Authcache模块原理详解
Authcache+Mobile Switch构建高性能Drupal站点

本文讲一下如果通过修改authcache的核心代码,来实现缓存页面的个性化内容。

Authcache p13n图示


通用的缓存,或多或少都是要进行个性化处理的,比如用户名显示、动态加载用户资料、用户好友等等。
一般情况下,这种局部个性化,都是通过两种手段实现:一个是SSI,另一个是CSI。
Authcache本身可以实现局部personalization, 模块叫p13n。
Authcache的ajax模块属于CSI,ESI模块应该是属于SSI,但是由于ESI模块需要搭建varnish服务器,配置VCL,加上服务器的设置问题,会导致ESI容易出错,并且本身ESI传递cookie也会有些问题,因此ESI实际上实现起来相当复杂。

具体的varnish相关技术,参见这几篇文章:

Varnish构建高负载Drupal网站 – 基本篇
Varnish构建高负载Drupal网站 – 高级篇
Varnish模块的作用是什么?
Varnish的简单配置
(PS:如果有相关问题,请到Drupal大学提问^_^)

所以,如果我们要使用服务器端的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) {
      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里面的内容了,达到了个性化页面的目的。

关于Authcache与Varnish以及ESI的高级用法,各位客观扫等,期待下篇内容归来 :)
更多问题,请到Drupal大学提问^_^



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复