用Memcache 缓存加速Mysql的查询V1.0
用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 "";
}
?>
完成!
六月 30th, 2010 at 09:23
这个,要服务器支持才可以。。。。。。
六月 17th, 2011 at 05:40
My Recommended Websites…
[...]some new websites on the web we love, even if they are not related to ours. Check them out[...]…