在线文档1:https://docs.qq.com/doc/DQVdadWNzcWJBSWdB
Memcache常用命令:https://docs.qq.com/doc/DQVh5dWtQSXVjdE9n
<?php
#实例化一个类
$mem = new Memcache();
#连接memache服务器
$mem->connect('localhost',11211);
//添加Username的键,值是libai,不压缩,缓存120秒
// $mem->add('username','libai',0,120);
// $mem->add('age','12',0,120);
echo $mem->get('username');
echo 'ok';
案例1
<?php
$mem = new Memcache();
$mem->connect('localhost',11211);#获取缓存数据
$data = $mem->get('data');#判断缓存中是否有数据
if(!$data){echo 'a';$pdo = new PDO('mysql:host=localhost;dbname=itcast','root','root');$pdo->exec('set names utf8');$sql = 'select * from news';$res = $pdo->query($sql);$data = $res->fetchAll(PDO::FETCH_ASSOC);#数据写入缓存$mem->set('data',$data,0,3600);}
echo '<pre>';
print_r($data);