memcached +mysql+php 例子

<?php
header("content-type:text/html;charset=utf-8");
$memcachehost = '127.0.0.1'; 
$memcacheport = 11211; 
$memcachelife = 60; 
$memcache = new Memcache; 
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect"); 
$query="select * from personal_info limit 10"; 
$key=md5($query); 
if(!$memcache->get($key)) 
{ 
                $conn=mysql_connect("localhost","root","passwd"); 
                mysql_select_db(mydb); 
                $result=mysql_query($query); 
                while ($row=mysql_fetch_assoc($result)) 
                { 
                        $arr[]=$row; 
                } 
                $f = 'mysql'; 
                $memcache->add($key,serialize($arr),0,30);        //mysql 查询后,插入 memcached 
                $data = $arr ; 
} 
else{ 
        $f = 'memcache'; 
        $data_mem=$memcache->get($key); 
        $data = unserialize($data_mem); 
} 
echo $f; 
echo "<br>"; 
//print_r($data); 
foreach($data as $a) 
{ 
                echo "number is <b><font color=#FF0000>$a[pi_id]</font></b>"; 
                echo "<br>"; 
                echo "name is <b><font color=#FF0000>$a[pi_name]</font></b>"; 
                echo "<br>"; 
                echo "tel is <b><font color=#FF0000>$a[pi_tel]</font></b>"; 
                echo "<br>"; 
                echo "qq is <b><font color=#FF0000>$a[pi_qq]</font></b>"; 
                echo "<br>"; 
                echo "email is <b><font color=#FF0000>$a[pi_email]</font></b>"; 
                echo "<br>"; 
 
} 
?>

 

你可能感兴趣的:(memcached +mysql+php 例子)