<?php
error_reporting(E_ALL);
define ('DEF_MC_PREFIX', "app10"); //MC的前缀
include_once '../libs/sinainterface.class.php';
//include_once '../../libs/applogger.class.php';
require_once '../libs/mysqlpdo.class.php';
require_once '../config/db_config1.php';
require_once '../config/commstr1.php';
$test3 = new orderlist(0);
$test3->set_memcache_values(1);
print_r($test3->get_memcache_values(1));
?>
<?php
class OrderList
{
private $uid;//自己的uid号,取好友要用到
private $obj_mc;
private $mcop = true;//假如有了memcache就是true,否则没有true,不写缓存
public $int_all_order_count;
private $int_friend_order_count;
private $return_param;
function __construct($uid)
{
$this->uid = $uid;
$this->s_id = APP_SID;
$this->obj_db = new mysqlpdo ( );
if ($this->mcop) {
$re = $this->ConnectMc ( $mobj );
if ($re) {
$this->obj_mc = $mobj;
} else {
$this->mcop = false;
}
}
}
public function get_memcache_values($int_page)
{
if ($this->mcop)
{
$arr_subject = $this->obj_mc->get ( DEF_MC_PREFIX . APP_SID . '_' . $int_page );
$arr_acount = $this->obj_mc->get(DEF_MC_PREFIX . "_c" .APP_SID);
if ($arr_subject&&$arr_acount)
{
$this->int_all_order_count = $arr_acount ;
return $arr_subject;
}
}else{
//取数据库和调用set_memcache_values()进行赋值
//$this->obj_mc->set(DEF_MC_PREFIX . "_c".APP_SID, $arr_count [0] ['p_count'] , 0 ,60*60*12); //对memcache赋值。
}
}
public function set_memcache_values($int_page)
{
$arr_subject=array("uid" => "1212", "content" => "this ia a test by xiangdong...");
if($this->mcop)
{ //把循环出来的$arr_subject放入MC变量(MC有规则的含$i,以后便于取)
$this->obj_mc->set ( DEF_MC_PREFIX . APP_SID . '_' . $int_page, $arr_subject, 0 ,60 * 60 * 12 );
}
return 1;
}
public function ConnectMc(&$mcobj)
{
global $MC_CONFIG;
echo $MC_CONFIG;
$server = explode ( ":", $MC_CONFIG );
$mcobj = new Memcache ( );
$re = $mcobj->connect ( $server [0], $server [1] );
if (! $re)
return 0;
return 1;
}
}
?>
<?php
?>
error_reporting(E_ALL);
define ('DEF_MC_PREFIX', "app10"); //MC的前缀
include_once '../libs/sinainterface.class.php';
//include_once '../../libs/applogger.class.php';
require_once '../libs/mysqlpdo.class.php';
require_once '../config/db_config1.php';
require_once '../config/commstr1.php';
$test3 = new orderlist(0);
$test3->set_memcache_values(1);
print_r($test3->get_memcache_values(1));
?>
<?php
class OrderList
{
private $uid;//自己的uid号,取好友要用到
private $obj_mc;
private $mcop = true;//假如有了memcache就是true,否则没有true,不写缓存
public $int_all_order_count;
private $int_friend_order_count;
private $return_param;
function __construct($uid)
{
$this->uid = $uid;
$this->s_id = APP_SID;
$this->obj_db = new mysqlpdo ( );
if ($this->mcop) {
$re = $this->ConnectMc ( $mobj );
if ($re) {
$this->obj_mc = $mobj;
} else {
$this->mcop = false;
}
}
}
public function get_memcache_values($int_page)
{
if ($this->mcop)
{
$arr_subject = $this->obj_mc->get ( DEF_MC_PREFIX . APP_SID . '_' . $int_page );
$arr_acount = $this->obj_mc->get(DEF_MC_PREFIX . "_c" .APP_SID);
if ($arr_subject&&$arr_acount)
{
$this->int_all_order_count = $arr_acount ;
return $arr_subject;
}
}else{
//取数据库和调用set_memcache_values()进行赋值
//$this->obj_mc->set(DEF_MC_PREFIX . "_c".APP_SID, $arr_count [0] ['p_count'] , 0 ,60*60*12); //对memcache赋值。
}
}
public function set_memcache_values($int_page)
{
$arr_subject=array("uid" => "1212", "content" => "this ia a test by xiangdong...");
if($this->mcop)
{ //把循环出来的$arr_subject放入MC变量(MC有规则的含$i,以后便于取)
$this->obj_mc->set ( DEF_MC_PREFIX . APP_SID . '_' . $int_page, $arr_subject, 0 ,60 * 60 * 12 );
}
return 1;
}
public function ConnectMc(&$mcobj)
{
global $MC_CONFIG;
echo $MC_CONFIG;
$server = explode ( ":", $MC_CONFIG );
$mcobj = new Memcache ( );
$re = $mcobj->connect ( $server [0], $server [1] );
if (! $re)
return 0;
return 1;
}
}
?>
<?php
?>
参考:http://blog.csdn.net/chinalinuxzend/archive/2007/10/07/1813599.aspx
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);
?>
//访问统计
$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));
?>
使用压缩:$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);
?>
$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)
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>’;
}
}
?>
执行结果:$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(); //不成功,记录失败信息
}
?>
//设置篇
if($data = $memcache->get(’k’,$v)) {
//显示我们的数据
}
else {
$data = get_from_database; //得到数据源
if(!$memcache->set(’k’,$data), MEMCACHE_COMPRESSED) //开始设置
log(); //不成功,记录失败信息
}
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1298/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2008-10-13 14:33
评论列表