实例一:
<?php
//使用memcache类来操作
$mm = new Memcache();
$mm->addServer("192.168.70.114",11211);
//向连接池中添加一个memcache服务器
$mm->addServer("192.168.70.249",11211);
$mm->addServer("192.168.70.222",11211);
$mm->addServer("192.168.70.52",11211);
$mm->add("var_m1","zhangsan",MEMCACHE_COMPRESSED,10000);
//添加一个字串
$mm->set("var_m2",array("aaa","bbbb","cccc"),MEMCACHE_COMPRESSED,10000);//添加一个数组
$mm->set("var_m3",new
Stu(),MEMCACHE_COMPRESSED,10000);//添加一个对象
echo $mm->get("var_m1"); //获取信息并输出
echo
"<br/><br/>";
var_dump($mm->get("var_m2"));
echo
"<br/><br/>";
$ob = $mm->get("var_m3");
echo $ob->getinfo();
echo
"<br/><br/>";
$mm->close();
class Stu{
public
$name="qq";
public $age=20;
public function getinfo(){
echo $this->name.":".$this->age;
}
}
?>
实例二:
<?php
//使用memcache类来提高数据的查询速度
$mm = new Memcache();
$mm->addServer("127.0.0.1",11211); //向连接池中添加一个memcache服务器
$mm->addServer("192.168.70.249",11211);
$mm->addServer("192.168.70.222",11211);
$mm->addServer("192.168.70.52",11211);
//
$sql = "select * from stu";
$data = $mm->get(md5($sql)); //尝试以sql语句的md5值作为key从memcache中获取数据
if(!$data){
echo "执行查询--{$sql}<br/>";
//没有数据
$pdo = new PDO("mysql:host=localhost;dbname=lamp40","root","root");
$stmt = $pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);//以关联式数组取出所有数据
//将获取的信息缓存到memcache中
$mm->add(md5($sql),$data,MEMCACHE_COMPRESSED,3600);
$mm->set("stu",md5($sql),MEMCACHE_COMPRESSED,3600);
}
//$mm->set(md5($sql),$data,MEMCACHE_COMPRESSED,3);
//输出数据
foreach($data as $stu){
echo "{$stu['id']}:{$stu['name']}<br/>";
}
$mm->close();
?>
public $age=20;
public function getinfo(){
echo $this->name.":".$this->age;
}
}
?>
实例二:
<?php
//使用memcache类来提高数据的查询速度
$mm = new Memcache();
$mm->addServer("127.0.0.1",11211); //向连接池中添加一个memcache服务器
$mm->addServer("192.168.70.249",11211);
$mm->addServer("192.168.70.222",11211);
$mm->addServer("192.168.70.52",11211);
//
$sql = "select * from stu";
$data = $mm->get(md5($sql)); //尝试以sql语句的md5值作为key从memcache中获取数据
if(!$data){
echo "执行查询--{$sql}<br/>";
//没有数据
$pdo = new PDO("mysql:host=localhost;dbname=lamp40","root","root");
$stmt = $pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);//以关联式数组取出所有数据
//将获取的信息缓存到memcache中
$mm->add(md5($sql),$data,MEMCACHE_COMPRESSED,3600);
$mm->set("stu",md5($sql),MEMCACHE_COMPRESSED,3600);
}
//$mm->set(md5($sql),$data,MEMCACHE_COMPRESSED,3);
//输出数据
foreach($data as $stu){
echo "{$stu['id']}:{$stu['name']}<br/>";
}
$mm->close();
?>