Drupal7中配置Memcache方法详解

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

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

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

3.配置settings.php

实例代码如下:

  1. $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  2. // The 'cache_form' bin must be assigned no non-volatile storage.
  3. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  4. $conf['cache_default_class'] = 'MemCacheDrupal';
  5. $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实例的话,需要添加如下配置,代码如下:

  1. $conf['memcache_servers'] = array(
  2. '10.1.1.1:11211' => 'default',
  3. '10.1.1.1:11212' => 'default',
  4. '10.1.1.2:11211' => 'default',
  5. '10.1.1.3:11211' => 'cluster2',
  6. '10.1.1.4:11211' => 'cluster2'
  7. );
  8. $conf['memcache_bins'] = array(
  9. 'cache' => 'default',
  10. 'cache_filter' => 'cluster2',
  11. 'cache_menu' => 'cluster2'
  12. );

一切就绪,最后讲解一下单台机器如何配置多个memcache实例,即单台机器的memcache集群搭建。

我们需要修改2个文件,一个是启动脚本/etc/init.d/memcached-multi,另一个是配置文件/etc/sysconfig/memcached,代码如下:

/etc/sysconfig/memcached

是用来配置多个memcache实例的大小,代码如下:

  1. PORT="11211"
  2. USER="memcached"
  3. MAXCONN="1024"
  4. CACHESIZE="512"
  5. OPTIONS=""
  6. MULTIBUCKET="2"
  7. CACHESIZEARRAY[1]="1024"
  8. CACHESIZEARRAY[2]="128"/etc/init.d/memcached-multi

是用来启动memcache集群,(start、stop、restart),代码如下:

  1. #! /bin/sh
  2. #
  3. # chkconfig: - 55 45
  4. # description: The memcached daemon is a network memory cache service.
  5. # processname: memcached
  6. # config: /etc/sysconfig/memcached
  7. # pidfile: /var/run/memcached/memcached.pid
  8. # Standard LSB functions
  9. #. /lib/lsb/init-functions
  10. # Source function library.
  11. . /etc/init.d/functions
  12. PORT=11211
  13. USER=memcached
  14. MAXCONN=1024
  15. CACHESIZE=64
  16. OPTIONS=""
  17. MULTIBUCKET=""
  18. if [ -f /etc/sysconfig/memcached ];then
  19. . /etc/sysconfig/memcached
  20. fi
  21. [ -z "$MULTIBUCKET" ] && MULTIBUCKET=1
  22. # Check that networking is up.
  23. . /etc/sysconfig/network
  24. if [ "$NETWORKING" = "no" ]
  25. then
  26. exit 0
  27. fi
  28. RETVAL=0
  29. prog="memcached"
  30. start () {
  31. echo -n $"Starting $prog: "
  32. # insure that /var/run/memcached has proper permissions
  33. if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
  34. chown $USER /var/run/memcached
  35. fi
  36. for i in `/usr/bin/seq 1 $MULTIBUCKET`; do
  37. THISCACHESIZE=$CACHESIZE
  38. [ ${#CACHESIZEARRAY[*]} -gt 0 -a ${CACHESIZEARRAY[$i]:-0} -gt 0 ] && THISCACHESIZE=${CACHESIZEARRAY[$i]}
  39. 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
  40. let RETVAL=$RETVAL+$?
  41. let PORT=$PORT+1
  42. done
  43. echo
  44. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
  45. }
  46. stop () {
  47. echo -n $"Stopping $prog: "
  48. for i in `/usr/bin/seq 1 $MULTIBUCKET`; do
  49. killproc -p /var/run/memcached/memcached-$i.pid /usr/bin/memcached
  50. let RETVAL=$RETVAL+$?
  51. done
  52. echo
  53. if [ $RETVAL -eq 0 ] ; then
  54. rm -f /var/lock/subsys/memcached
  55. rm -f /var/run/memcached.pid
  56. fi
  57. }
  58. restart () {
  59. stop
  60. start
  61. }
  62. # See how we were called.
  63. case "$1" in
  64. start)
  65. start
  66. ;;
  67. stop)
  68. stop
  69. ;;
  70. status)
  71. echo "Warning: This status check is laughable. Inspect netstat or ps output manually."
  72. status memcached
  73. ;;
  74. restart|reload|force-reload)
  75. restart
  76. ;;
  77. condrestart)
  78. [ -f /var/lock/subsys/memcached ] && restart || :
  79. ;;
  80. *)
  81. echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  82. exit 1
  83. esac
  84. exit $RETVAL

至此一些就绪.