install memcached 1.4.13 on CentOS 6.2

- Make sure to check PHP-FPM and MYSQL posts.

- One of the dependencies of memcache is libevent, get latest version from libevent.org

mkdir libevent
cd libevent
wget https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar xfz libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable

- get latest version from memcached.org


mkdir memcached
cd memcached
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar xfz memcached-1.4.13.tar.gz
cd memcached-1.4.13
ln /usr/local/lib/libevent-2.0.so.5  /lib/
ln /usr/local/lib/libevent-2.0.so.5.1.5 /lib/
ldconfig
./configure
make
make install

- add init script

#adding directory for memcached socket
mkdir /var/run/memcached
chmod 777 /var/run/memcached
vi /etc/init.d/memcached
#!/bin/bash
#
# Init file for memcached
# description: Distributed memory caching daemon
#
# processname: memcached
#
# Source function library.
. /etc/rc.d/init.d/functions
 
RETVAL=0
PROG=memcached
PIDFILE=/var/run/$PROG/$PROG.pid
DAEMON=/usr/local/bin/memcached
 
start() {
	echo -n $"Starting $PROG: "
	#use linux socket
	daemon  --pidfile $PIDFILE $DAEMON  -P $PIDFILE -d -u www -m 100 -c 3000 -s /var/lib/memcached/memcached.sock -v 2>> /var/log/memcached.log
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$PROG
	return $RETVAL
}
 
stop() {
	echo -n $"Shutting down $PROG: "
	killproc -p $PIDFILE memcached 
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
	return $RETVAL
}
 
restart() {
	stop
	start
}
 
reload() {
	echo -n $"Reloading $PROG: "
	killproc -p $PIDFILE $PROG -HUP
	RETVAL=$?
	echo
	return $RETVAL
}
 
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condrestart)
	[ -e /var/lock/subsys/$PROG ] && restart
	RETVAL=$?
	;;
  reload)
	reload
	;;
  status)
	status -p $PIDFILE $PROG
	RETVAL=$?
	;;
   *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	RETVAL=1
esac
exit $RETVAL

- start memcached

chmod +x /etc/init.d/memcached 
/etc/init.d/memcached start

- install memcache extension for PHP

pecl install memcache
cp /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so /usr/local/lib/

- add memcached to php.ini

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
extension=memcache.so
memcache.chunk_size = 132768
memcache.allow_failover = 0

- test memcached
- create new memcached_test.php file and add this code

<?php
 
//set cache
if( _cache( 'i', 'cached string' ) )
        echo "Cache added\n";
//get cache
$i = _cache('i');
echo "\$i = {$i} n";
//del cache
if( _cache( 'i', -1 ) )
        echo "Cache deleted\n";
 
 
 
/*
 * Handles cache using memcached
 * @param string $name : var name
 * @param mixed $val : NULL to get the cache value
 *                     -1 to delete cache
 *                     mixed to set cache
 * @param int $ttl : cache time to live
 * @return mixed : cahe result
 */
 
 
function _cache( $name, $val=NULL, $ttl=432000 ){
    global $mcdb;
 
    if(empty( $mcdb ))
        //Connecting using linux socket
        $mcdb = memcache_connect('unix:////var/run/memcached/memcached.sock', 0);
 
    //delete
    if($val === -1){
        return memcache_delete($mcdb,$name);
    //set
    }elseif( $val !== NULL ){
        return memcache_set($mcdb,$name,$val, false, $ttl);
   //get
    }else{
        $retval = memcache_get($mcdb,$name);
        return  $retval ? $retval : NULL;
    }
 
    return NULL;
}
 
?>

- test memcached

/etc/init.d/memcached start
php -c /etc/php.ini -f /ser/memcached_test.php

- result should look like this

Cache added
$i = cached string 
Cache deleted

Recommended posts: