# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure –with-libevent=/usr
# make
# make install
安装完成后会把 memcached 放到 /usr/local/bin/memcached
-->测试是否成功安装 memcached:
memcache
memcache support enabled
Version 3.0.1
Revision $Revision: 1.83.2.24 $
Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 32768 32768
memcache.default_port 11211 11211
memcache.hash_function crc32 crc32
memcache.hash_strategy consistent consistent
memcache.max_failover_attempts 20 20
memcache.protocol ascii ascii
memcache.redundancy 1 1
memcache.session_redundancy 2 2
说明我们的扩展以成功安装并运行,下面开始测试吧!
新建 example.php 文件:
1
2 <?php
3
4 $memcache = new Memcache;
5 $memcache->connect('localhost', 11211) or die ("Could not connect");
6
7 $version = $memcache->getVersion();
8 echo "Server's version: ".$version."<br/>\n";
9
10 $tmp_object = new stdClass;
11 $tmp_object->str_attr = 'test';
12 $tmp_object->int_attr = 123;
13
14 $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
15 echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
16
17 $get_result = $memcache->get('key');
18 echo "Data from the cache:<br/>\n";
19
20 var_dump($get_result);
21
22 ?>
看看输出结果是不是:
Server's version: 1.4.5
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) }
如果是的话,那就欢呼吧~~~成功啦!
关于 Memcache 的配置及其使用说明,请参考 PHP 手册的 Memcache Functions 这一章,介绍的很详细!