memcached安装+php使用手记

memcached 安装 +php 使用手记
本文简要介绍一下安装的情况,以及PHP模块memcache使用情况:
提要:
1。安装 memcached 服务器端
2。安装 php memcache 支持模块
3。使用 memcache 情况,计数器、数据压缩
4。 Memcache 内存的更新清理 (delete flush)
5。内存超量的测试 (set)
1。安装 memcached 服务器端
memcached
安装说明 ( 北南南北的站 )
http://www.linuxsir.org/main/?q=node/184 
注意的是:如果你安装过程中出现错误,请看看是不是有模块没装:
autoconf
zlib (
压缩数据用 )

2。安装 php memcache 支持模块
PHP
老家:
http://cn.php.net/manual/zh/ref.memcache.php
(PHP 4.3.3 or newer is required to use the memcache extension.
难道, 4.3.3 都装上了吗?好像没有吧,用 4.4.4 好像都要另外装的 )

3。使用 memcache 情况,计数器、数据压缩
 
使用情况一:统计
<?php
// 访问统计
$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  ’ 访问结果为: ’. $s ;
?>
其实我们可以用 increment 方法代替上面的做法
<?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);
?>
 
数据压缩  
<?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);
?>

使用情况说明:
前台比较
目前内存
bytes
总共读取
bytes_read
总共写入
bytes_written
压缩前
700085
700081
416
压缩后
1131
1125
13
可能看到压缩后明显占用内存少了不少
 
4。 Memcache 内存的更新清理 (delete flush)
<?php
$memcache  =  new  Memcache;
$memcache ->connect(’localhost’, 11211) or  die  ("Could not connect");

/* 设置值 */
$status  =  $memcache ->getStats();
echo  ’ 设置前内存使用情况 ’. $status [’bytes’].’<br>’;
echo  ’ 设置后 ’;
for ( $i =0; $i <9; $i ++) {
    
$memcache ->set(’b’. $i , rand (1,99));    
    
echo  ’<br>’. $i .’->’. $memcache ->get(’b’. $i );       
}

/* 查看设置的值 */
$status  =  $memcache ->getStats();
echo  ’delete 前内存使用情况 ’. $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 前内存使用情况 ’. $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 后内存使用情况 ’. $status [’bytes’].’<br>’;
?>
 
 
5。内存超量的测试 (set)
 我们把内存设为2M
./memcached -d -m 2 -p 11211 -u root
 
<?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 .’-> 设置成功 <br>’;
        
$status  =  $memcache ->getStats();
        
echo  ’ 内存 :’. $status [’bytes’].’<br>’;
    }
    
else  {
        
echo  $j .’-> 设置失败 <br>’;
    }
}
?>
执行结果:
test1->设置成功
内存:600042
test2->设置成功
内存:1200084
test3->设置失败
test4->设置成功
内存:1200084
test5->设置失败
test6->设置失败
 
刚好印证我们的计算,不过20万的repeat为什么会失败,不是太了解,,,,,,
 
总结:
示例:
<?
// 设置篇
if ( $data  =  $memcache ->get(’k’, $v )) {
    
// 显示我们的数据
    }
else  {
    
$data  = get_from_database;  // 得到数据源
     if (! $memcache ->set(’k’, $data ), MEMCACHE_COMPRESSED)  // 开始设置
     log ();     // 不成功 , 记录失败信息     
}
?>
-----------OVER-----------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值