🦄 2024 独立开发者训练营,一起创业!(早鸟优惠在6天后结束)查看介绍 / 立即报名 →

创建高性能 Drupal 应用:使用 Memcached

Memcached,是一套使用内存作为缓存的系统,可以有效的缓解数据库的压力,被很多大型网站广泛使用,Youtube,Twitter,WordPress.com 等等,Memcached 是高性能网站的标准配置。我们可以配置一下,让 Drupal 也可以使用 Memcached,需要用到 Drupal 的 Memcache API and Integration 模块。这个简单的配置手册,说明了在 CentOS 服务器上安装并配置 Memcached ,并让 Drupal 可以使用它。

一、容易混淆的概念

Memcached ,是一个程序,可以运行在服务器上,就像运行 nginx,apache,或者 mysql 那样。PECL memcache 还有 memcached 是两个不太一样的东西,它们的作用其实就是去跟 Memcached 服务进行沟通。另外,Drupal 有个模块叫 Memcache API and Integration (memcache),它的作用是让 Drupal 使用 PECL memcache 或者 memcached 去跟 Memcached 服务进行对话。

二、安装 Memcached 服务

使用 yum 可以安装 Memcached,安装之前你也可以搜索一下资源库里是否有 Memcached:

yum search memcached

如果有,继续使用 yum 去安装,如果没有,你需要添加资源库到你的系统里。

yum install memcached

三、安装 PECL memcache 扩展

Drupal 的 Memcache API and Integration 模块,推荐使用 PECL memcache,并且要求最低版本是 3.0.6 或以上。这里我们得再去为系统添加一个资源库,才能安装高版本的 PECL memcache,可以安装 ius 资源库:

wget http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ius-release-1.0-11.ius.centos6.noarch.rpm

然后:

rpm -ivh ius-release-1.0-11.ius.centos6.noarch.rpm

这样我们就可以使用 yum 命令,去安装 PECL memcache 了,你可以先去搜索一下,看看都有哪些可用的关于 memcache 的资源。这里我需要使用 php5.3 版本的 PECL memcache,所以可以这样:

yum install php53u-pecl-memcache

完成以后,重启一下  php-fpm , nginx  还有 memcached 服务,如果你用的是 apache 服务器的话,也需要重启 apache 服务。

service php-fpm restart
service nginx restart
service memcached restart

四、安装 Drupal 的 Memcache API and Integration 模块

如果你想在正式的网站上安装 Memcache API and Integration 模块,你需要先开启 Drupal 的维护模块:admin/config/development/maintenance,然后使用 drush 或者在 Drupal 的模块管理界面去安装这个模块。

安装完成以后,我们得去修改一下 Drupal 的 settings.php ,这是 Drupal 的配置文件,默认是在sites/default/settings.php ,可以使用 vim 之类的命令去编辑一下这个文件。然后添加下面这几行代码:

// Make memcache the default cache class
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';

// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

// Tell Drupal not to bootstrap the database when serving cached pages to anonymous visitors
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;

$conf['memcache_key_prefix'] = 'ninghao1_';

memcache_key_prefix 这个配置是 memcache 所使用的前缀,就是如果多个 Drupal 网站使用同一个 Memcached 的话,可以分别去设置不同的前缀。这样可以区分一下。

在 admin/config/system/memcache 可以开启 memcache 的统计,这样在页面底部,你会看到 memcache 的使用情况。

五、可能会遇到的错误

在启用 Drupal 的 memcache 模块时,出现错误提示:

Notice: Undefined index: description 在 drupal_check_module() (行 1225 在 /home/www/nginx.ninghao.net/includes/install.inc).
(当前使用 Memcache Required PHP extension not found. Install the memcache (recommended) or memcached extension.)

这是因为,你还没有安装 PECL memcache 扩展。

打开 状态报告 时,报错:

Notice: Undefined index: value 在 theme_status_report() (行 2584 在 /home/www/nginx.ninghao.net/modules/system/system.admin.inc).

这应该是你还没有去修改 settings.php 文件。

Drupal memcache

评论

刚刚发现这个网站,准备了解一下。还有,最近打算订阅支持一下,虽然很少有时间上来学习。
视频很清晰,请问阿皓同学是用什么软件录制的?

嘿嘿。谢谢了。我用的是 ScreenFlow 。

皓哥研究一下redis,据说比memcache出色很多,完了教教大家。最近写了很多内容,辛苦了。

memcache. 和 varnish 不可以同时安装吧?刚刚看到你教学varnish安完 在安装 memcache.,感觉这个部分重复了!
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_backends'][] = 'sites/all/modules/varnish/varnish.cache.inc';

是啊。这个实验好难啊。

非常感谢皓哥的分享,
我的模块安装与服务器配置好像成功了,运行STATS命令,返回信息:
stats
STAT pid 4556
STAT uptime 3054554609
STAT time 387740552
STAT version 1.4.4-14-g9c660c0
STAT pointer_size 64
STAT curr_connections 30
STAT total_connections 34
STAT connection_structures 31
STAT cmd_get 482883
STAT cmd_set 127919
STAT cmd_flush 0
STAT get_hits 353216
STAT get_misses 129667
STAT delete_misses 27
STAT delete_hits 19
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0

STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 221893307
STAT bytes_written 1357628285
STAT limit_maxbytes 2147483648
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 33967170
STAT curr_items 47711
STAT total_items 127919
STAT evictions 0
END
问题是视图打开速度没有加快,甚至降低,2000多视图,测试3遍了,一直没效果,该怎样排查问题,望皓哥赐教。

是不是新版本的 Drupal 配置方法有变化呀,您有没有参考这个模块的文档。在 Drupal 7 安装了这个模块以后,它有个简单的配置页面,上面会显示 Show memcache statistics at the bottom of each page ,勾选以后,会提示一些相关信息。

或者您说一下用的 Drupal 的版本,我在本地可以测试一下。

我用的D7.8

哦,Drupal 7 应该用的就是文章里介绍的方法,您可以再参考一下模块的文档,看看是不是配置方法有变化。

微信好友

用微信扫描二维码,
加我好友。

微信公众号

用微信扫描二维码,
订阅宁皓网公众号。

240746680

用 QQ 扫描二维码,
加入宁皓网 QQ 群。

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点