安装memcache
cat test.php
<?php
$memcachehost = 'xx.xx.xx.xx'; memcache主机
$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("xxx.xxx.xxx.xxx","test","test"); //mysql主机也可在同
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);exit;
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>";
}
?>
##########################################
在mysql主机上
mysql>create database mydb;
mysql>
CREATE TABLE `personal_info` (
`pi_id` bigint(20) NOT NULL auto_increment,
`pi_name` varchar(50) NOT NULL,
`pi_tel` varchar(15) default NULL,
`pi_qq` varchar(15) default NULL,
`pi_email` varchar(50) default NULL,
PRIMARY KEY (`pi_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
创建一个表
######################################
mysql>INSERT INTO `mydb`.`personal_info` (
`pi_id` ,
`pi_name` ,
`pi_tel` ,
`pi_qq` ,
`pi_email`
)
VALUES (
'1', 'wmy', '13522221231', '55555555', 'wmy@nginxs.com');
插入数据
#################
注意如果不是在一个服务器注意mysql要对 test用户授权
测试了。。。