header('Content-Type:text/html; charset=utf-8'); // 经过 Memcache 类 连接 $memcache = new Memcache(); // 连接 Memcache $memcache->connect('localhost', 11211) or die('连接失败'); $sql = "SELECT id,username FROM mh_member order by id desc limit 10"; $key = md5($sql); $data = $memcache->get($key); if(empty($data)){ // PDO 连接数据库 try{ $pdo = new PDO("mysql:host=localhost;dbname=taijiyc", "root", "", array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES UTF8')); $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "数据库链接失败:".$e->getMessage(); } $sql = "SELECT id,username FROM mh_member order by id desc limit 10"; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt -> fetchAll(PDO::FETCH_ASSOC); $memcache -> set($key, $data, MEMCACHE_COMPRESSED, 5); echo '第一次执行,将数据缓存到内存中'; } echo '<pre>'; print_r($data); echo '</pre>'; $memcache->close();