Sunday, December 4, 2011

Setup for Linux Memcached (daemon) with PHP PECL Memcache and Drupal Memcache Module - with localized UNIX sockets

This information enhances some documentation for the Drupal Project Memcache -> http://drupal.org/project/memcache
Revision Notes PHP: I have tested both PECL Memcache and PECL Memcached versions, and for the moment see less 'bugs' with the use of PECL memcache.
Suggested 'priority' - is to get the latest [memcache] release at http://svn.php.net/viewvc/pecl/memcache/
It has several improved 'features' - including the option to use faster 'localized' Unix 'sockets'

Memcache - Guidelines: see items 1) to 3)

1) A typical Drupal / Pressflow 6.2x use of memcache - via settings.php is: (copied form my own Linux server + based on fast [local] sockets for memcache)
<?php/**
* Memcache:
*/
$conf += array(
 
'memcache_extension'           => 'memcache',
 
'show_memcache_statistics'     => 0,
 
'memcache_persistent'          => TRUE,
 
'memcache_stampede_protection' => TRUE,
 
'memcache_stampede_semaphore'  => 15,
 
'memcache_stampede_wait_time'  => 5,
 
'memcache_stampede_wait_limit' => 3,
 
'memcache_key_prefix'          => basename(realpath(conf_path())),
);
// We don't use chained memcached caching for sites cron, php-cli and install!if (isset($_SERVER['HTTP_USER_AGENT']) && !preg_match("/(?:cron\.php|install\.php)/", $_SERVER['REQUEST_URI'])) {
  if (isset(
$_SERVER['HTTP_HOST'])) {
   
$conf += array(
     
'cache_inc'        => './sites/all/modules/memcache/memcache.inc',
     
'session_inc'      => './sites/all/modules/memcache/memcache-session.inc',
     
'lock_inc'         => './sites/all/modules/memcache/memcache-lock.inc',
     
'memcache_servers' => array(
        
'unix:///tmp/memcached.socket0:0'  => 'default',
        
'unix:///tmp/memcached.socket1:0'  => 'block',
        
'unix:///tmp/memcached.socket2:0'  => 'content',
        
'unix:///tmp/memcached.socket3:0'  => 'filter',
        
'unix:///tmp/memcached.socket4:0'  => 'form',
        
'unix:///tmp/memcached.socket5:0'  => 'menu',
        
'unix:///tmp/memcached.socket6:0'  => 'page',
        
'unix:///tmp/memcached.socket7:0'  => 'update',
        
'unix:///tmp/memcached.socket8:0'  => 'views',
        
'unix:///tmp/memcached.socket9:0'  => 'session',
        
'unix:///tmp/memcached.socket10:0' => 'users'
     
),
     
'memcache_bins'    => array(
        
'cache'         => 'default',
        
'cache_block'   => 'block',
        
'cache_content' => 'content',
        
'cache_filter'  => 'filter',
        
'cache_form'    => 'form',
        
'cache_menu'    => 'menu',
        
'cache_page'    => 'page',
        
'cache_update'  => 'update',
        
'cache_views'   => 'views',
        
'session'       => 'session',
        
'users'         => 'users'
     
),
    );
  }
}
?>
2) Sample 'memcache php.ini' settings:
[memcache]
; Data will be transferred in chunks of this size
memcache.chunk_size = 32768
memcache.hash_strategy = consistent
memcache.default_port = 0
session.save_handler = memcache
session.save_path = "unix:///tmp/memcached.socket11:0?persistent=1&weight=1&timeout=1&retry_interval=15"
3) Sample Linux memcached daemon /etc/init.d/memcached script:
#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached-multi daemon is a network memory cache service.
# processname: memcached-multi
# 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
UDP=0
SOCKET=/tmp/memcached.socket
VAR=0
USER=memcached
MAXCONN=300
CACHESIZE=64
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then
        . /etc/sysconfig/memcached
fi

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
        exit 0
fi

RETVAL=0
prog="memcached"

start_instance() {
        echo -n $"Starting $prog ($1): "
#       daemon --pidfile /var/run/memcached/memcached.$1.pid memcached -d -p $PORT           -u $USER -m $2 -c $MAXCONN -P /var/run/memcached/memcached.$1.pid $OPTIONS
        daemon --pidfile /var/run/memcached/memcached.$1.pid memcached -d -s $3 -a 766 -L -t 8 -u $USER -m $2 -c $MAXCONN -P /var/run/memcached/memcached.$1.pid $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached.$1
}

stop_instance() {
        echo -n $"Stopping $prog ($1): "
        killproc -p /var/run/memcached/memcached.$1.pid /usr/bin/memcached
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f /var/lock/subsys/memcached.$1
            rm -f /var/run/memcached.$1.pid
        fi
}


start() {
    # insure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
        chown $USER /var/run/memcached
    fi
    # we start 12 socket streams for memcached
    start_instance default 64 /tmp/memcached.socket0;
    start_instance block 64 /tmp/memcached.socket1;
    start_instance content 64 /tmp/memcached.socket2;
    start_instance filter 64 /tmp/memcached.socket3;
    start_instance form 64 /tmp/memcached.socket4;
    start_instance menu 64 /tmp/memcached.socket5;
    start_instance page 64 /tmp/memcached.socket6;
    start_instance update 64 /tmp/memcached.socket7;
    start_instance views 64 /tmp/memcached.socket8;
    start_instance session 64 /tmp/memcached.socket9;
    start_instance users 64 /tmp/memcached.socket10;
    start_instance pbold 64 /tmp/memcached.socket11;
}

stop () {
    stop_instance default;
    stop_instance block;
    stop_instance content;
    stop_instance filter;
    stop_instance form;
    stop_instance menu;
    stop_instance page;
    stop_instance update;
    stop_instance views;
    stop_instance session;
    stop_instance users;
    stop_instance pbold;
}

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status memcached
        ;;
  restart|reload|force-reload)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
        exit 1
esac

exit $?
Notes: Above examples taken from my own working Linux server (multi-hosts).
For the last option (Linux), I used "yum install memcached"
eg:
yum search memcached
>
memcached.x86_64 : High Performance, Distributed Memory Object Cache



Additional Notes: The PHP Memcache PECL can also be compiled fully 'static' into the php-5.3.x binary-by moving the [memcache - PECL] source into the php /ext folder and rebuilding the PHP 'buildconf' - then compile PHP 5.3.x with --enable-memcache (it is *not* essential to use PECL if you are after the best speed - and understand *static* PHP compile builds).
PHP 5.3.x Static Compile guidelines:

In the PHP source root folder, issue the following (Linux);
rm configure
PHP_AUTOCONF=autoconf-2.13 PHP_AUTOHEADER=autoheader-2.13 ./buildconf --force


Then compile PHP manually (or use rpmbuild or similar). I usually work direct with Fedora RPM sources, hence I used; rpmbuild -bb php.spec >> php.checklist 2>&1

Using Unix Sockets with the Drupal Memcache Project:

To use Linux sockets with the Drupal memcache module - please refer to: http://drupal.org/node/538426
This method works well for me on my multi-hosted Linux dedicated server.

Current Changelog for PHP PECL memcache

: from http://pecl.php.net/package-changelog.php?package=memcache&release=3.0.6
3.0.6 - Fixed PECL Bug #16672 (memcache.php doesn't support unix socket)
- Fixed PECL Bug #19374 (memcache.php throws Notice: Undefined index: VALUE when viewing expired items)
- Fixed PECL Bug #17518 (Strange behavior in increment on non integer and after)
- Fixed potential segfault in memcache queue.
- Fixed various tests




Revison #1 Doc's by peter bowey http://drupal.org/user/564804

Peter Bowey Computer Solutions
http://www.pbcomp.com.au/

No comments:

Post a Comment