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 '';print_r($data);echo '';$memcache->close();