主页 > Drupal > 通过Hack来提高Boost模块的性能

通过Hack来提高Boost模块的性能

PDF版本

Boost模块,Drupal高性能网站中的必备利器,前文《Drupal性能优化之-将Boost模块用到极致》详细讲解了几处对Boost模块的优化。但是近期笔者通过对一个大型Drupal网站的性能跟踪,发现Boost模块本身会有性能问题,本文就几点Boost的使用经验做个总结。

1. 防止Boost清除缓存
Boost模块实现了hook_nodeapi,在node每次更新、保存等操作的时候,Boost都会清除cache。Boost清除内存是一个极其费时的操作,在许多场合下,我们不需要清除缓存的操作,比如content_profile的操作。

在node_save的时候,Drupal默认有一个选项,是否清除缓存,代码如下:

  // Clear the page and block caches.
  if (variable_get('node_save_clear_cache', TRUE)) {
    cache_clear_all();
  }

同样,可以把这个操作带到Boost,在boost.module模块里面,修改nodeapi如下,

function boost_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if (!BOOST_ENABLED) return;
 
  //Don't clear the cache if we set it FALSE
  if (!variable_get('node_save_clear_cache', TRUE)) {
    return;
  }

这样,在node_save的时候,设置node_save_clear_cache为false即可。(注意:不要用variable_set)

global $conf;
$conf['node_save_clear_cache'] = FALSE;
node_save($node);
...

2. 让登录用户的访问也产生Boost缓存
默认情况下,登录用户的访问不会生产Boost的静态缓存。但是有时,登录用户也需要缓存某些页面。比如,所有登录用户相同调查页面、欢迎页面。或者登录用户访问的页面和匿名用户访问的页面完全一样,则可以共享静态页面缓存。

这里我们可以通过修改Boost模块,让登录用户的访问也产生缓存,修改boost_init函数的末尾位置:

  //Check $conf variable
  //Determine if we should cache file
  $GLOBALS['_boost_should_cached'] = FALSE;
  $must_cache_files = isset($GLOBALS['conf']['cache_boost']) ? $GLOBALS['conf']['cache_boost'] : FALSE;
  if ($must_cache_files) {
    if (is_string($must_cache_files)) {
      $must_cache_files = array($must_cache_files);
    }
    foreach ($must_cache_files as $cache_file ) {
      if ($cache_file)
      if (drupal_match_path($_GET['q'], $cache_file)) {
        $GLOBALS['_boost_should_cached'] = TRUE;
        break;
      }
    }
  }
 
  // We only generate cached pages for anonymous visitors.
  if (empty($user->uid) || $GLOBALS['_boost_should_cached']) {
    if (BOOST_ENABLED != CACHE_AGGRESSIVE) {
      $GLOBALS['conf']['cache'] = CACHE_DISABLED;
    }
    $GLOBALS['_boost_cache_this'] = TRUE;
    register_shutdown_function('_boost_ob_handler');
    ob_start();
  }

添加了如上面的代码之后,如果想要强行缓存某些页面(无论是否是匿名/登录用户访问),在settings.php里面添加如下设置,

$conf['cache_boost'][] = '/page1';
$conf['cache_boost'][] = '/page2/*';

3. 让登录用户读取boost缓存
默认情况,对于登录用户的访问,用户不会读取boost的缓存页面,前文《Drupal性能优化之-将Boost模块用到极致》中讲解过如何修改apache配置文件让登录用户访问Boost缓存。但是如果我们不需要对所有登录用户使用静态缓存,只需要对某个页面的话,则需要简单如下设置。

  ## Cache for special page
  RewriteCond %{REQUEST_URI} /page/1
  RewriteRule .* - [S=1]
 
  # Caching for anonymous users
  # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server OR https request
  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [OR]
  RewriteCond %{REQUEST_URI} (^/(admin|cache|misc|modules|sites|system|openid|themes|node/add))|(/(comment/reply|edit|user|user/(login|password|register))$) [OR]
  RewriteCond %{HTTP_COOKIE} DRUPAL_UID [OR]
  RewriteCond %{HTTP:Pragma} no-cache [OR]
  RewriteCond %{HTTP:Cache-Control} no-cache [OR]
  RewriteCond %{HTTPS} on
  RewriteRule .* - [S=1]

以上是Boost模块的几个小小修改,以便来提高Drupal的性能,其他文章可以参考:
- Drupal性能优化之-将Boost模块用到极致
- 让猪去飞-漫谈Drupal性能优化经验贴


声明: 本站所有文章欢迎转载,所有文章未说明,均属于原创,转载均请注明出处。
本文有效链接: http://www.drupal001.com/2012/05/hack-boost/
版权所有: Drupal与高性能网站架构 http://www.drupal001.com


, ,

评论:1

发表评论
  1. avatar
    回复 任锦峰
    16/09/17

    之前使用boost,提速效果很不错,虽然性能不及varnish,但胜在系统提供,易于使用。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注


4 − = 一

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
通过Hack来提高Boost模块的性能 来自 Drupal与高性能网站架构
顶部