用Memcache 缓存加速Mysql的查询V1.0

六月 28th, 2010 Posted in PHP | 阅读次数: 327 次

用Memcache缓存加速Mysql的查询,很不错的接口,希望对大家有所帮助!

<?php
$memcachehost = '192.168.1.1';
$memcacheport = 12000;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) \
	or die ("Could not connect");
$query="select * from user limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
        $conn=mysql_connect("localhost","root","passwd");
        mysql_select_db(users);
        $result=mysql_query($query);
        while ($row=mysql_fetch_assoc($result))
        {
            $arr[]=$row;
        }
        $f = 'db';
        $memcache->add($key,serialize($arr),0,30);
        $data = $arr ;
}
else{
        $f = 'mem';
    $data_mem=$memcache->get($key);
    $data = unserialize($data_mem);
}
echo $f;
echo "";
//print_r($data);
foreach($data as $a)
{
        echo $a[user_id]._.$a[email];
        echo "";
}
?>

 

完成!

相关日志:

Tags: ,

2 Responses to “用Memcache 缓存加速Mysql的查询V1.0”

  1. 荒野无灯 Says:

    这个,要服务器支持才可以。。。。。。


  2. hemorhhoid relief Says:

    My Recommended Websites…

    [...]some new websites on the web we love, even if they are not related to ours. Check them out[...]…


留下您的脚印