Memcached - PHP 安装&使用

来源:互联网 发布:淘宝联盟等级划分 编辑:程序博客网 时间:2024/04/29 14:34

下载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服务端

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

安装完毕重启服务:

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

Memcache的内定Function
connect(ip, port)
set(key, value)  数据压缩:set(key, value, MEMCACHE_COMPRESSED)
get(key)
increment, 同set(好像多了个计数功能,没试过)
getStats() 返回当前内存占用情况(return array) print_r($memcached->getStats());
flush  清空内存占用.

代码范例(来自chinaunix):

PHP代码
  1. <?php  
  2. //访问统计  
  3. $memcache = new Memcache;  
  4. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  5. if($s=$memcache->get(’a’)) {  
  6.     $s=$s+1;  
  7.     $memcache->set(’a’,$s);  
  8. }  
  9. else  
  10. $memcache->set(’a’,1);  
  11. echo ’访问结果为:’.$s;  
  12. ?>  
等效
PHP代码
  1. <?php  
  2. $memcache = new Memcache;  
  3. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  4.   
  5. if($s=$memcache->increment(’a’,1)) {  
  6.     echo $s;      
  7. }  
  8. else  
  9. $memcache->set(’a’,1);  
  10. ?>  
数据压缩
PHP代码
  1. <?php  
  2. $memcache = new Memcache;  
  3. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  4. $test=(str_repeat(’jetwong’,100000));  
  5. $memcache->set(’b’,($test));  
  6. ?>  
  7. 使用压缩:  
  8. <?php  
  9. $memcache = new Memcache;  
  10. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  11. $test=(str_repeat(’jetwong’,100000));  
  12. $memcache->set(’b’,($test),MEMCACHE_COMPRESSED);  
  13. ?>  
内存的更新清理
PHP代码
  1. <?php  
  2. $memcache = new Memcache;  
  3. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  4.   
  5. /*设置值*/  
  6. $status = $memcache->getStats();  
  7. echo ’设置前内存使用情况’.$status[’bytes’].’<br>’;  
  8. echo ’设置后’;  
  9. for($i=0;$i<9;$i++) {  
  10.     $memcache->set(’b’.$i,rand(1,99));      
  11.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i);         
  12. }  
  13.   
  14. /*查看设置的值*/  
  15. $status = $memcache->getStats();  
  16. echo ’delete前内存使用情况’.$status[’bytes’].’<br>’;  
  17. echo ’<br>开始delete’;  
  18. for($i=0;$i<9;$i++) {  
  19.     $memcache->delete(’b’.$i);      
  20.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i);  
  21. }  
  22.   
  23. /*查看flush使用的情况*/  
  24. $status = $memcache->getStats();  
  25. echo ’使用flush前内存使用情况’.$status[’bytes’].’<br>’;  
  26. echo ’使用flush情况:’;  
  27. for($i=0;$i<9;$i++) {  
  28.     $memcache->set(’b’.$i,rand(1,99));      
  29.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i);    
  30. }  
  31. $memcache->flush();  
  32. echo ’flush之后:’;  
  33. for($i=0;$i<9;$i++) {          
  34.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i);  
  35. }  
  36. $status = $memcache->getStats();  
  37. echo ’flush后内存使用情况’.$status[’bytes’].’<br>’;  
  38. ?>  
内存超量测试
PHP代码
  1. <?php  
  2. $memcache = new Memcache;  
  3. $memcache->connect(’localhost’, 11211) or die ("Could not connect");  
  4.   
  5. //600K左右  
  6. $test1str_repeat(’jetlee’,100000);  
  7. //600K左右  
  8. $test2str_repeat(’jetlee’,100000);  
  9. //600K左右  
  10. $test3str_repeat(’李连杰’,200000);  
  11. //600K左右  
  12. $test4str_repeat(’连杰李’,100000);  
  13. //200K  
  14. $test5file_get_contents(’http://img.pconline.com.cn/images/photoblog/2988177/20068/4/1154688770042_mthumb.JPG’);  
  15. $test6file_get_contents(’http://img.pconline.com.cn/images/photoblog/1767557/20069/28/1159417108902_mthumb.jpg’);  
  16.   
  17. for($i=1;$i<=6;$i++) {  
  18.     $j=’test’.$i;  
  19.     if($memcache->set($j,$$j)) {  
  20.         echo $j.’->设置成功<br>’;  
  21.         $status = $memcache->getStats();  
  22.         echo ’内存:’.$status[’bytes’].’<br>’;  
  23.     }  
  24.     else {  
  25.         echo $j.’->设置失败<br>’;  
  26.     }  
  27. }  
  28. ?>  
总结
PHP代码
  1. <?  
  2. //设置篇  
  3. if($data = $memcache->get(’k’,$v)) {  
  4.     //显示我们的数据  
  5.     }  
  6. else {  
  7.     $data = get_from_database; //得到数据源  
  8.     if(!$memcache->set(’k’,$data), MEMCACHE_COMPRESSED) //开始设置  
  9.     log();    //不成功,记录失败信息      
  10. }  
  11. ?> 

原创粉丝点击