<?php
//创建一个memcache对象
$mem=new memcache();
//建立一个memcache服务器连接
$mem->connect("localhost",11211);
$key='mydata';
//查询一个memcache服务器中缓存的值
$data=$mem->get($key);
//如果memcache查询结果为空则连接数据库取出数据
if(empty($data)){
try{
$pdo=new PDO("mysql:host=localhost;dbname=bookstore",'root',158369);
}catch(PDOException $e){
echo "数据库连接失败!失败原因为:".$e->getmessage();
}
$sql='select id,name,age,educatino,salary from salary order by id';
$statement=$pdo->prepare($sql);
$statement->execute();
$data=$statement->fetchALL(PDO::FETCH_ASSOC);
$mem->set($key,$data,MEMCACHE_COMPRESSED,60);
echo "这是第一次访问数据库且将访问内容存到了内存中<br>";
}
echo '<pre>';
print_r($data);
echo '</pre>';
//关闭连接
$mem->close();
//关闭数据库连接
$pdo=null;
?>
原文链接:https://blog.csdn.net/living_ren/article/details/77871956
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~