MemCached 安装
1、安装Memcached2、安装memcache PHP模块
3、测试脚本
一、环境需求
装Memcached需要libevent库的支持,所以请在安装Memcached之前检查有没有安装libevent。测试环境还需要PHP的支持,
本文假设PHP已经安装到/usr/local/php目录下,也就是在编译PHP的时候使用perfix参数指定目录(--prefix=
/usr/local/php)
二、下载相关软件
Memcached下载地址 http://www.danga.com/memcached/
memcache PHP模块下载地址 http://pecl.php.net/package/memcache 推荐使用1.5版
libevent 下载地址 http://www.monkey.org/~provos/libevent/
# tar vxzflibevent-1.3e.tar.gz
# cd libevent-1.1a
#./configure
# make
# make install
建立一个符号连接:#ls -s /usr/local/lib/libevent-1.1.so.1 /usr/lib
2、安装Memcached
# tar memcached-1.2.8.tar.gz
# cd memcached-1.2.8
# ./configure --prefix=/usr/local/memcached
--with-libevent=/usr
# make
# make install
# cd /usr/local/php/modules/memcached/bin
# ./memcached -d -m 50 -p 11211 -u root
参数说明 -m 指定使用多少兆的缓存空间;-p 指定要监听的端口; -u 指定以哪个用户来运行
然后重启web服务器即可。如果安装成功,则通过phpinfo()可以获得该扩展的相关信息:
memcache support
enabled
Active persistent connections
0
Version
2.2.5
Revision
$Revision: 1.111 $
Directive
Local Value
Master Value
memcache.allow_failover
1
1
memcache.chunk_size
8192
8192
memcache.default_port
11211
11211
memcache.default_timeout_ms
1000
1000
memcache.hash_function
crc32
crc32
memcache.hash_strategy
standard
standard
memcache.max_failover_attempts
20
20
3、安装memcache PHP模块
# tar vxzf memcache-2.0.4.tgz
# cd memcache-2.0.4
# /usr/local/php/bin/phpize
如果出现:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.
错误原因是没有安装autoconf,你必须安装以下两个rpm:
rpm -ivh /media/cdrom/Server/imake-1.0.2-3.i386.rpm
rpm -ivh /media/cdrom/Server/autoconf-2.59-12.noarch.rpm
#./configure --with-php-config=/usr/local/php/bin/php-config --with-apxs=/usr/local/apache/bin/apxs --with-gettext --enable-socket --enable-memcache --enable-sysvshm --enable-shmop
# make
# make install
然后修改php.ini
把
extension_dir = "./"
修改为
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并添加一行
extension=memcache.so
3、测试脚本
自己写一个PHP程序测试一下吧
Code
<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)\n";
$get_result = $memcache->get('key');
echo "Data from the cache:\n";
var_dump($get_result);
?>
运行后输出如下:
Server's version: 1.2.8
Store data in the cache (data will expire in 10 seconds)
Data from the cache: object(stdClass)#3 (2)
{ ["str_attr"]=>string(4) "test" ["int_attr"]=>int(123) }
页:
[1]