一,WHY we install memcached扩展 for php5
memcached的1.2.4及以上增加了CAS(Check and Set)协议
php的扩展memcache,不支持cas,所以要装memcached扩展(http://pecl.php.net/package/memcached ),memcached扩展是基于libmemcached,所以要先安装libMemcached(http://libmemcached.org/libMemcached.html);
libMemcached is designed to provide the greatest number of options to use Memcached. Some of the features provided:
Asynchronous and Synchronous Transport Support.
Consistent Hashing and Distribution.
Tunable Hashing algorithm to match keys.
Access to large object support.
Local replication.
A complete reference guide and documentation to the API.
Tools to Manage your Memcached networks.
Memcached()支持getMulti()和setMulti(),即存取一个ID LIST,分页列表中的一些统计数据CACHE是很好的应用场景;
二,查看memcahced的版本
telnet 127.0.0.1 11211
stats
+++++++++++
STAT pid 1000
STAT uptime 22415
STAT time 1322297562
STAT version 1.4.5
STAT pointer_size 32
STAT rusage_user 4.793271
STAT rusage_system 17.905277
STAT curr_connections 6
STAT total_connections 140122
STAT connection_structures 25
STAT cmd_get 191367
STAT cmd_set 22226
STAT cmd_flush 0
STAT get_hits 169139
STAT get_misses 22228
STAT delete_misses 1935
STAT delete_hits 679
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 37556795
STAT bytes_written 1165861842
STAT limit_maxbytes 268435456
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 1498608
STAT curr_items 1597
STAT total_items 22226
STAT evictions 0
STAT reclaimed 16075
END
如果版本过低,考虑重新装一下
退出telnet ,ctrl + ] 然后在按q就行了。
三,下载所需源码
Visit: https://launchpad.net/libmemcached/+download
wget http://launchpad.net/libmemcached/1.0/0.42/+download/libmemcached-0.42.tar.gz
vist: http://pecl.php.net/package/memcached
wget http://pecl.php.net/get/memcached-2.0.0b2.tgz
memcached server side的官方网站 http://www.memcached.org/
四,安装libmemcached
tar zxvf libmemcached-0.42.tar.gz
cd libmemcached-0.42
./configure –prefix=/usr/local/libmemcached –with-memcached
make && make install
五,php的扩展memcached的安装
tar zxvf memcached-1.0.2.tar.gz
cd memcached-2.0.0
/usr/local/php/bin/phpize
./configure –enable-memcached –with-php-config=/usr/local/php/bin/php-config –with-libmemcached-dir=/usr/local/memcached
./configure –prefix=/usr/local/phpmemcached –with-memcached –with-php-config=/usr/local/php/bin/php-config –with-libmemcached-dir=/usr/local/bmemcached
make && make install
###############
Build complete.
Don’t forget to run ‘make test’.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
linux-3q12:/home/yejun/memcached-2.0.0b2 # cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
#################
vi /usr/local/php/lib/php.ini
在文件最后几行加上extension=memcached.so重起一下HTTP服务,如:apachectl restart就可以了。
<?php
Phpinfo();
?>
看看效果,上面是老的MEMCACHE模块,下面是新扩展的:
六:看看新的接口
Memcached — The Memcached class
七:使用场景
public mixed
Memcached::getMulti ( array $keys [, array &$cas_tokens [, int $flags ]] )
Memcached::getMulti() is similar to Memcached::get(), but instead of a single key item, it retrieves multiple items the keys of which are specified in the keys array. If cas_tokens variable is provided, it is filled with the CAS token values for the found items.
Note:
Unlike Memcached::get() it is not possible to specify a read-through cache callback for Memcached::getMulti(), because the memcache protocol does not provide information on which keys were not found in the multi-key request.
<?php
$m = new Memcached();
$m->addServer(‘localhost’, 11211);
$items = array(
‘key1′ => ‘value1′,
‘key2′ => ‘value2′,
‘key3′ => ‘value3′
);
$m->setMulti($items);
$result = $m->getMulti(array(‘key1′, ‘key3′, ‘badkey’), $cas);
var_dump($result, $cas);
?>
<?php
$m = new Memcached();
$m->addServer(‘localhost’, 11211);
$items = array(
‘key1′ => ‘value1′,
‘key2′ => ‘value2′,
‘key3′ => ‘value3′
);
$m->setMulti($items, time() + 300);
?>