Skip navigation

Using memcache to store objects is not a new strategy to reduce database load and improve responsiveness of an application, but it is an often overlooked option due to the ‘extra’ labor of installation and configuration. Hopefully the following information will make this easier for some and me to remember.

Installing and configuring a memcache server on linux:

  1. Download, configure, and compile memcache.
  2. Create a startup script:
    #! /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=2048
    CACHESIZE=128
    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"
    pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
    lockfile=${LOCKFILE-/var/lock/subsys/memcached}
    #
    start () {
    echo -n $"Starting $prog: "
    # Ensure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
    chown $USER /var/run/memcached
    fi
    #
    daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
    }
    stop () {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
    rm -f ${lockfile} ${pidfile}
    fi
    }
    # restart
    restart () {
    stop
    start
    }
    # See how we were called.
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status -p ${pidfile} memcached
    RETVAL=$?
    ;;
    restart|reload|force-reload)
    restart
    ;;
    condrestart|try-restart)
    [ -f ${lockfile} ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
    RETVAL=2
    ;;
    esac
    exit $RETVAL
    
  3. Add the daemon to the service manager, and enable it for runlevel3:
    • chkconfig –add –level 3 memcached
  4. Start the Memcache server:
    • service memcached start

You should now have a functioning memcache server, available on the localhost, port 11211. you can check to see if it’s running with service memcached status. Now, to use it within PHP for example you have to provide the PHP extension for memcache so that you can store and retrieve data.  For this purpose, there is a PECL extension available here: http://pecl.php.net/package/memcache

Building the extension is fairly easy:

  1. wget http://pecl.php.net/get/memcache-3.0.8.tgz
  2. tar -xvzf memcache-3.0.8.tgz
  3. cd memcache-3.0.8
  4. phpize
  5. ./configure && make && make install
  6. Check /etc/php.d/memcached.ini, if it doesn’t exist or correctly point to the compiled extension (*.so) PHP will not be able to communicate with the memcache server.

Now the easy part -> using it. For that, RTFM. http://php.net/manual/en/book.memcache.php

Leave a Reply