一、memcached 的编译安装
准备:
Memcached下载地址:http://www.danga.com/memcached/
libevent下载地址: http://monkey.org/~provos/libevent/

安装步骤:

  1. 下载libevent & memcached 源码包
1
2
libevent-1.3.tar.gz
memcached-1.2.2.tar.gz
  1. 解压缩libevent-1.3.tar.gz,编译安装
1
2
3
4
# tar zxvf  libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure —prefix=/usr/local/libevent
# make ; make install
  1. 解压缩 memcached-1.2.2.tar.gz, 编译安装
1
2
3
4
# tar zxvf memcached-1.2.2.tar.gz 
# cd memcached-1.2.2
# ./configure —prefix=/usr/local/memchached —with-libevent=/usr/local/libevent
# make;make install
  1. 作软连接,否则运行memcached的时候将找不到libevent模块
1
# ln -s /usr/local/libevent/lib/libevent-1.3.so.1 /usr/lib

5.启动memcached

1
# /usr/local/memcached/bin/memcached -d -m 256 -p 11211 -u root

OK !!! 搞定! ^^

二、php的memcached模块

1
2
3
4
5
6
# tar vxzf memcache-1.5.tgz
# cd memcache-1.5
# /usr/local/php/bin/phpize
# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
# make
# make install

安装完后会有类似这样的提示:

1
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/

把这个记住,然后修改php.ini,把

1
extension_dir = "./"

修改为

1
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"

并添加一行

1
extension=memcache.so

3、测试脚本
自己写一个PHP程序测试一下吧

1
2
3
4
5
6
7
8
9
10
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
//连接Memcached服务器
$memcache->set('key', 'test');
//设置一个变量到内存中,名称是key值是test
$get_value = $memcache->get('key');
//从内存中取出key的值
echo $get_value;
?>