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

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. else 
  9. $memcache->set(’a’,1); 
  10. echo ’访问结果为:’.$s
  11. ?> 
等效
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. else 
  8. $memcache->set(’a’,1); 
  9. ?> 
数据压缩
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. $status = $memcache->getStats(); 
  15. echodelete前内存使用情况’.$status[’bytes’].’<br>’; 
  16. echo ’<br>开始delete’; 
  17. for($i=0;$i<9;$i++) { 
  18.     $memcache->delete(’b’.$i);     
  19.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); 
  20.  
  21. /*查看flush使用的情况*/ 
  22. $status = $memcache->getStats(); 
  23. echo ’使用flush前内存使用情况’.$status[’bytes’].’<br>’; 
  24. echo ’使用flush情况:’; 
  25. for($i=0;$i<9;$i++) { 
  26.     $memcache->set(’b’.$i,rand(1,99));     
  27.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i);   
  28. $memcache->flush(); 
  29. echoflush之后:’; 
  30. for($i=0;$i<9;$i++) {         
  31.     echo ’<br>’.$i.’->’.$memcache->get(’b’.$i); 
  32. $status = $memcache->getStats(); 
  33. echoflush后内存使用情况’.$status[’bytes’].’<br>’; 
  34. ?> 
内存超量测试
PHP代码
  1. <?php 
  2. $memcache = new Memcache; 
  3. $memcache->connect(’localhost’, 11211) or die ("Could not connect"); 
  4.  
  5. //600K左右 
  6. $test1= str_repeat(’jetlee’,100000); 
  7. //600K左右 
  8. $test2= str_repeat(’jetlee’,100000); 
  9. //600K左右 
  10. $test3= str_repeat(’李连杰’,200000); 
  11. //600K左右 
  12. $test4= str_repeat(’连杰李’,100000); 
  13. //200K 
  14. $test5= file_get_contents(’http://img.pconline.com.cn/images/photoblog/2988177/20068/4/1154688770042_mthumb.JPG’); 
  15. $test6= file_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. ?> 
总结                           
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. ?> 

 

 

 

http://www.php.net/manual/zh/book.memcached.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值