欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

给Drupal使用更强劲的缓存利器-Redis

shiping1 的头像
关于Memcache和Redis的区别,本文不打算做过多的讨论。从理论上讲,如果drupal的Redis模块写得够合理(没有细看源码,粗略估计一下),Redis对drupal的性能提升肯定比Memcache要大,单就数据结构上的扩展,就省去了很多Memcache中复杂的操作,外加上Redis的持久化,可以做部分存储使用,因此可以代替部分数据库的功能,此外做过Memcache性能研究的用户更会发现使用Memcache的瓶颈并不在于速度,而是数据结构处理。所以,我们打算使用Redis来做Drupal的缓存。

安装Redis
我们使用yum或者apt安装,比较简单,具体略过。
安装完成之后,redis的配置文件位于:/etc/redis.conf,可以设置持久化策略、内存使用等,由于redis支持VM策略,因此内存的瓶颈应该不是什么大问题。(注意一下pid的路径,以后要用到)

启动Redis

/etc/init.d/redis-server start

启动成功之后,我们就可以通过redis自带的命令行工具redis-cli进入交互界面,如果可以进入,就表示启动成功。

安装PHP扩展
Redis的PHP扩展有两个:Predis和PhpRedis
我们打算使用PhpRedis,因为它有PECL包,可以直接安装使用,并且兼容PHP5.2
PECL安装的话就比较简单了。

pecl install redis

安装完成,我们来做个简单的PHP+Redis的测试

$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->set('hello','hello world!');
echo $redis->get('hello');

输出hello world!即可。也可以第二次把set语句注释掉,看看能不能get到值。

安装Drupal的Redis模块
我们默认使用Drupal7,由于Redis模块不支持Drupal6,因此在Drupal6上使用Redis要使用Cache Backport,https://drupal.org/project/cache_backport,具体操作,请参阅相关文档。

drush dl redis

注意:redis模块无须启动,启动redis模块,只是为一些第三方依赖模块提供一个设置界面,因此我们不打算启动Redis模块。

Drupal的Redis设置
参考了网上某篇牛人的设置文件,稍微修改一下,放在settings.php最后,具体如下(注意修改redis.pid文件及路径):

if (file_exists('/var/run/redis/redis.pid')) {
  $redis_up = TRUE;
}
if (file_exists('sites/all/modules/contrib/entitycache/entitycache.info')) {
  $entity_cache = TRUE;
}
 
if (!empty($redis_up)) {
  $conf['lock_inc'] = 'sites/all/modules/contrib/redis/redis.lock.inc';
  $conf['redis_cache_socket'] = '/var/run/redis/redis.sock';
 
  // Default behavior for all bins, prefix is 'mysite_'.
  $conf['cache_prefix']['default'] = 'mysite_';
 
 // Required configurations.
  $conf['lock_inc'] = 'sites/all/modules/contrib/redis/redis.lock.inc';
  $conf['cache_backends'][] = 'sites/all/modules/contrib/redis/redis.autoload.inc';
  $conf['redis_client_interface'] = 'PhpRedis';
  $conf['redis_client_base'] = 1;
  $conf['redis_client_host'] = '127.0.0.1';
  $conf['redis_client_port'] = '6379';
  // $conf['cache_prefix'] = 'mysite_';
 
  // Optional not redis specific.
  // $conf['cache_lifetime'] = 0;
  // $conf['page_cache_max_age'] = 0;
  // $conf['page_cache_maximum_age'] = 0;
  $conf['page_cache_invoke_hooks'] = TRUE;
  $conf['page_cache_without_database'] = FALSE;
  // $conf['redis_client_password'] = 'isfoobared';
 
  // Cache bins.
  $conf['cache_default_class'] = 'Redis_Cache';
  $conf['cache_bootstrap'] = 'Redis_Cache';
  $conf['cache_class_cache'] = 'Redis_Cache';
  $conf['cache_class_cache_menu'] = 'Redis_Cache';
  $conf['cache_class_cache_block'] = 'Redis_Cache';
  $conf['cache_class_cache_views'] = 'Redis_Cache';
  $conf['cache_class_cache_views_data'] = 'Redis_Cache';
  $conf['cache_field'] = 'Redis_Cache';
  $conf['cache_filter'] = 'Redis_Cache';
  $conf['cache_image'] = 'Redis_Cache';
  $conf['cache_libraries'] = 'Redis_Cache';
  $conf['cache_metatag'] = 'Redis_Cache';
  $conf['cache_search_api_solr'] = 'Redis_Cache';
 
  // Always Database Cache.
  $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
 
  // Entity Cache.
  if (!empty($entity_cache)) {
    $conf['cache_entity_node'] = 'Redis_Cache';
    $conf['cache_entity_fieldable_panels_pane'] = 'Redis_Cache';
    $conf['cache_entity_file'] = 'Redis_Cache';
    $conf['cache_entity_taxonomy_term'] = 'Redis_Cache';
    $conf['cache_entity_taxonomy_vocabulary'] = 'Redis_Cache';
  }
 
}

测试
安装前后我们分别通过redis-cli来获取数据。

redis 127.0.0.1:6379KEYS *
) "test"
redis 127.0.0.1:6379[1]:KEYS *
) "mysite_:cache_bootstrap:hook_info"
) "mysite_:cache:schema:runtime:1"
) "mysite_:cache_menu:links:shortcut-set-1:tree-data:en:9bd1605e2280833450478f9083b7f8714c2fa28f1012455e2744e5af1a13eec5"
) "mysite_:cache_menu:links:shortcut-set-1:page:node:en:1:0"
) "mysite_:cache_bootstrap:system_list"
) "mysite_:cache_bootstrap:module_implements"
...

也可以通过INFO命令获取

db0:keys=1,expires=0
db1:keys=29,expires=29

注意一点:默认是db0,笔者的drupal存到了db1上,因此需要执行 SELECT 1 来切换当前DB到db1上。

如果要还要测试,可以通过 TYPE命令得到数据类型,如 TYPE mysite_:cache:schema:runtime:1,再通过相应的获取函数,如 HGETALL mysite_:cache:schema:runtime:1 得到所有值,具体就不再赘述。

有一篇文章《Why we recommend redis as a Drupal caching backend》,大概讲了使用Redis的好处,文中使用newrelic的数据做了对比,如下图所示:

不过,这只是使用Redis前后的对比,并没有使用Memcache和Redis的对比,因此这种性能的提升不用论证就能得到肯定答案,意义不大。以后的篇幅里,给大家做一个Memcache和Redis性能的对比。

如何在Drupal7中配置Memcache
Drupal自定义缓存之共享内存

来自  http://ju.outofmemory.cn/entry/56179
来自  http://www.drupal001.com/2013/12/drupal-redis/
推荐参考:

 

普通分类: