2025 独立开发者训练营:AI Agent!查看介绍 / 立即报名 →

创建高性能 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
微信好友

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

微信公众号

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

240746680

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

统计

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

社会化网络

关于

微信订阅号

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