本文章來給各位同學詳細介紹關於Drupal7中配置Memcache方法詳解,希望此方法對各位同學會有所幫助哦。
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
至此一些就緒。