Home >Backend Development >PHP Tutorial >Detailed explanation of how to configure Memcache in Drupal7_PHP tutorial
This article will give you a detailed introduction to the method of configuring Memcache in Drupal7. I hope this method will be helpful to you.
I won’t go into details about the configuration of Memcache. Drupal has many modules and frequent database calls, so memcache is a necessary configuration in the Drupal site. This article briefly lists the configuration of memcache in Drupal7 for your convenience.
1. Install memcache service and start memcached.
2. Install drupal’s memcache module. (http://drupal.org/project/memcache)
3. Configure 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'; |
Note that the file path of memcache.inc needs to be written correctly. As mentioned in the summary of Drupal module directory organization, we generally place the memcache module under the contrib folder, so the path may be sites/all/modules/ contrib/memcache/memcache.inc.
In addition, it is best to set memcache_key_prefix.
4. Multiple memcachd services.
By default, if memcache_servers and memcache_bins are not configured, Drupal will think that there is only one server, 127.0.0.1:11211. If there are multiple memcache instances, you need to add the following configuration.
The code is as follows | Copy code | ||||||||||||
$conf['memcache_servers'] = array(
'10.1.1.1:11212' => 'default', '10.1.1.2:11211' => 'default',
'10.1.1.3:11211' => 'cluster2',
'cache' => 'default', 'cache_filter' => 'cluster2',
|
The code is as follows | Copy code |
PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="512" OPTIONS="" MULTIBUCKET="2" CACHESIZEARRAY[1]="1024" CACHESIZEARRAY[2]="128"/etc/init.d/memcached-multi |
is used to start the memcache cluster. (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 "" 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: {start|stop|status|restart|reload|force-reload|condrestart}" |