Drupal7中配置Memcache方法详解

作者:袖梨 2022-06-25

Memcache的配置就不多说,drupal由于模块较多,数据库调用频繁,因此memcache是drupal站点中必备的配置,本文大概列一下Drupal7中关于memcache的配置情况,以方便大家查阅。

1. 安装memcache服务以及启动memcached。

2. 安装drupal的memcache模块。(http://drupal.org/project/memcache)

3. 配置settings.php

 代码如下 复制代码
 $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  // The 'cache_form' bin must be assigned no non-volatile storage.
  $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  $conf['cache_default_class'] = 'MemCacheDrupal';
  $conf['memcache_key_prefix'] = 'something_unique';

注意,需要将memcache.inc的文件路径写正确,如Drupal模块目录组织方式总结中提到,我们一般将memcache模块会放置在contrib文件夹下面,因此路径可能是 sites/all/modules/contrib/memcache/memcache.inc。
此外memcache_key_prefix最好设置一下。

4. 多个memcachd服务。
默认情况下,如果不配置memcache_servers以及memcache_bins的话,Drupal会认为只有一个server,即127.0.0.1:11211,如果有多个memcache实例的话,需要添加如下配置。

 代码如下 复制代码
$conf['memcache_servers'] = array(
  '10.1.1.1:11211' => 'default',
  '10.1.1.1:11212' => 'default',
  '10.1.1.2:11211' => 'default',
  '10.1.1.3:11211' => 'cluster2',
  '10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
  'cache'  => 'default',
  'cache_filter' => 'cluster2',
  'cache_menu' => 'cluster2'
);

一切就绪。

最后讲解一下单台机器如何配置多个memcache实例,即单台机器的memcache集群搭建。
我们需要修改2个文件,一个是启动脚本/etc/init.d/memcached-multi,另一个是配置文件/etc/sysconfig/memcached。

 代码如下 复制代码
/etc/sysconfig/memcached

是用来配置多个memcache实例的大小

 代码如下 复制代码
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="512"
OPTIONS=""
MULTIBUCKET="2"
CACHESIZEARRAY[1]="1024"
CACHESIZEARRAY[2]="128"/etc/init.d/memcached-multi

是用来启动memcache集群。(start、stop、restart)

 代码如下 复制代码
#! /bin/sh
#
# chkconfig: - 55 45
# description:    The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
 
# Standard LSB functions
#. /lib/lsb/init-functions
 
# Source function library.
. /etc/init.d/functions
 
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
MULTIBUCKET=""
 
if [ -f /etc/sysconfig/memcached ];then
    . /etc/sysconfig/memcached
fi
 
[ -z "$MULTIBUCKET" ] && MULTIBUCKET=1
 
# Check that networking is up.
. /etc/sysconfig/network
 
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi
 
RETVAL=0
prog="memcached"
 
start () {
    echo -n $"Starting $prog: "
    # insure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
        chown $USER /var/run/memcached
    fi
 
    for i in `/usr/bin/seq 1 $MULTIBUCKET`; do
        THISCACHESIZE=$CACHESIZE
        [ ${#CACHESIZEARRAY[*]} -gt 0 -a ${CACHESIZEARRAY[$i]:-0} -gt 0 ] && THISCACHESIZE=${CACHESIZEARRAY[$i]}
        daemon --pidfile /var/run/memcached/memcached.pid memcached -d -p $PORT -u $USER  -m $THISCACHESIZE -c $MAXCONN -P /var/run/memcached/memcached-$i.pid $OPTIONS
        let RETVAL=$RETVAL+$?
        let PORT=$PORT+1
    done
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo -n $"Stopping $prog: "
    for i in `/usr/bin/seq 1 $MULTIBUCKET`; do
        killproc -p /var/run/memcached/memcached-$i.pid /usr/bin/memcached
        let RETVAL=$RETVAL+$?
    done
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f /var/run/memcached.pid
    fi
}
 
restart () {
        stop
        start
}
 
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    echo "Warning: This status check is laughable.  Inspect netstat or ps output manually."
    status memcached
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart)
    [ -f /var/lock/subsys/memcached ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    exit 1
esac
 
exit $RETVAL

至此一些就绪。

相关文章

精彩推荐