Memcached - PHP 安裝&使用
下載Memcached:
http://danga.com/memcached/download.bml
安裝:
下載安裝libevent,已安裝可跳過:
下載地址:http://www.monkey.org/~provos/libevent/
Memcached For PHP Module下載地址:
http://pecl.php.net/package/memcache
http://cn.php.net/manual/zh/ref.memcache.php
安裝Memcached服務(wù)端
root@tonyvicky:# tar vxzf memcached-1.1.12.tar.gz
root@tonyvicky:# cd memcached-1.1.12
root@tonyvicky:# ./configure --prefix=/usr/local/memcached
root@tonyvicky:# make
root@tonyvicky:# make install
安裝完畢重啟服務(wù):
root@tonyvicky:# cd /usr/local/memcached/bin
root@tonyvicky:# ./memcached -d -m 50 -p 11211 -u root
安裝PHP模塊
root@tonyvicky:# tar vxzf memcache-1.5.tgz
root@tonyvicky:# cd memcache-1.5
root@tonyvicky:# /usr/local/php/bin/phpize
root@tonyvicky:# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
root@tonyvicky:# make
root@tonyvicky:# make install
安裝完后可能會有如下提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/
修改php.ini
extension_dir = "./"
修改為:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"
添加一行:
extension=memcache.so
安裝完畢,測試:
PHP代碼
<?php $memcache = new Memcache; //創(chuàng)建一個memcache對象 $memcache->connect('localhost', 11211) or die ("Could not connect"); //連接Memcached服務(wù)器 $memcache->set('key', 'test'); //設(shè)置一個變量到內(nèi)存中,名稱是key 值是test $get_value = $memcache->get('key'); //從內(nèi)存中取出key的值 echo $get_value;
Memcache的內(nèi)定Function
connect(ip, port)
set(key, value) 數(shù)據(jù)壓縮:set(key, value, MEMCACHE_COMPRESSED)
get(key)
increment, 同set(好像多了個計數(shù)功能,沒試過)
getStats() 返回當前內(nèi)存占用情況(return array) print_r($memcached->getStats());
flush 清空內(nèi)存占用.
代碼范例(來自chinaunix):
PHP代碼
<?php //訪問統(tǒng)計 $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); if($s=$memcache->get(’a’)) { $s=$s+1; $memcache->set(’a’,$s); } else $memcache->set(’a’,1); echo ’訪問結(jié)果為:’.$s; ?>
等效
PHP代碼
<?php $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); if($s=$memcache->increment(’a’,1)) { echo $s; } else $memcache->set(’a’,1); ?>
數(shù)據(jù)壓縮
PHP代碼
<?php $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); $test=(str_repeat(’jetwong’,100000)); $memcache->set(’b’,($test)); ?> 使用壓縮: <?php $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); $test=(str_repeat(’jetwong’,100000)); $memcache->set(’b’,($test),MEMCACHE_COMPRESSED); ?>
內(nèi)存的更新清理
PHP代碼
<?php $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); /*設(shè)置值*/ $status = $memcache->getStats(); echo ’設(shè)置前內(nèi)存使用情況’.$status[’bytes’].’<br>’; echo ’設(shè)置后’; for($i=0;$i<9;$i++) { $memcache->set(’b’.$i,rand(1,99)); echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); } /*查看設(shè)置的值*/ $status = $memcache->getStats(); echo ’delete前內(nèi)存使用情況’.$status[’bytes’].’<br>’; echo ’<br>開始delete’; for($i=0;$i<9;$i++) { $memcache->delete(’b’.$i); echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); } /*查看flush使用的情況*/ $status = $memcache->getStats(); echo ’使用flush前內(nèi)存使用情況’.$status[’bytes’].’<br>’; echo ’使用flush情況:’; for($i=0;$i<9;$i++) { $memcache->set(’b’.$i,rand(1,99)); echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); } $memcache->flush(); echo ’flush之后:’; for($i=0;$i<9;$i++) { echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); } $status = $memcache->getStats(); echo ’flush后內(nèi)存使用情況’.$status[’bytes’].’<br>’; ?>
內(nèi)存超量測試
PHP代碼
<?php $memcache = new Memcache; $memcache->connect(’localhost’, 11211) or die ("Could not connect"); //600K左右 $test1= str_repeat(’jetlee’,100000); //600K左右 $test2= str_repeat(’jetlee’,100000); //600K左右 $test3= str_repeat(’李連杰’,200000); //600K左右 $test4= str_repeat(’連杰李’,100000); //200K $test5= file_get_contents(’http://img.pconline.com.cn/images/photoblog/2988177/20068/4/1154688770042_mthumb.JPG’); $test6= file_get_contents(’http://img.pconline.com.cn/images/photoblog/1767557/20069/28/1159417108902_mthumb.jpg’); for($i=1;$i<=6;$i++) { $j=’test’.$i; if($memcache->set($j,$$j)) { echo $j.’->設(shè)置成功<br>’; $status = $memcache->getStats(); echo ’內(nèi)存:’.$status[’bytes’].’<br>’; } else { echo $j.’->設(shè)置失敗<br>’; } } ?>
總結(jié) PHP代碼
<? //設(shè)置篇 if($data = $memcache->get(’k’,$v)) { //顯示我們的數(shù)據(jù) } else { $data = get_from_database; //得到數(shù)據(jù)源 if(!$memcache->set(’k’,$data), MEMCACHE_COMPRESSED) //開始設(shè)置 log(); //不成功,記錄失敗信息 } ?>