关于memcache和redis的区别,本文不打算做过多的讨论。从理论上讲,如果 drupal 的redis模块写得够合理(没有细看源码,粗略估计一下),redis对 drupal 的性能提升肯定比memcache要大,单就数据结构上的扩展,就省去了很多memcache中复杂的操作,外加上redi
关于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:6379:keys *1) test
redis 127.0.0.1:6379[1]:keys * 1) mysite_:cache_bootstrap:hook_info 2) mysite_:cache:schema:runtime:1 3) mysite_:cache_menu:links:shortcut-set-1:tree-data:en:9bd1605e2280833450478f9083b7f8714c2fa28f1012455e2744e5af1a13eec5 4) mysite_:cache_menu:links:shortcut-set-1:page:node:en:1:0 5) mysite_:cache_bootstrap:system_list 6) mysite_:cache_bootstrap:module_implements...
也可以通过info命令获取
db0:keys=1,expires=0db1: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自定义缓存之共享内存
原文地址:给drupal使用更强劲的缓存利器-redis, 感谢原作者分享。