memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

http://www.danga.com/memcached/

系统: Centos 5.x

  1. 安装memcached 服务

安装: yum install -y libevent libevent-devel

wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz

tar xvzf memcached-1.2.6.tar.gz

cd memcached-1.2.6 && ./configure && make && make install

设置为系统服务

cp scripts/memcached.sysv /etc/init.d/memcached

chmod +x /etc/init.d/memcached

chkconfig –add memcached

chkconfig memcached on

因为默认安装到/usr/local/bin下,可以修改/etc/init.d/memcached

大约35行

daemon memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS

改为:

daemon /usr/local/bin/memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS

当然可以根据内存和服务器需求修改

MAXCONN=1024

CACHESIZE=128 (注:以MB为单位)

OPTIONS=”-l 127.0.0.1″ (注:因为是本机的缓存,可以限制监听ip)

service memcached start

  1. 配置memcache客户端,即php扩展memcache以连接memcached服务器。

http://pecl.php.net/package/memcache

wget http://pecl.php.net/get/memcache-2.2.4.tgz

tar xvzf memcache-2.2.4.tgz

cd memcache-2.2.4 && /path/to/phpize && ./configure –with-php-config=/path/to/php-config

make && make install

配置php.ini

修改 extension_dir = “/srv/php5/lib/php/extensions/no-debug-non-zts-20060613/”

添加 extension=”memcache.so”

[memcache]

memcache.dbpath=/var/lib/memcache

memcache.maxreclevel=0

memcache.maxfiles=0

memcache.archivememlim=0

memcache.maxfilesize=0

memcache.maxratio=0

mkdir -p /var/lib/memcache

/path/to/php -v 看看是否有错

php -v

PHP 5.2.8 (cli) (built: Dec  9 2008 15:42:34)

Copyright (c) 1997-2008 The PHP Group

Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

with XCache v1.2.2, Copyright (c) 2005-2007, by mOo

with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies

with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies

php -m

可以看到memcache扩展,那就ok了。

  1. 连接 ,修改php.ini

session.save_handler = memcache

session.save_path = “tcp://mem_cached_ip:11211”

  1. 重新启动服务器即可