- <?php
- phpinfo();
- header("Content-type: text/html; charset=utf-8");
- $db = new mysqli("localhost","root","","ceshi");
- $mem = new Memcache;
- $mflag = $mem->connect("localhost", 11211);
- //var_dump($db);
- $sql = "select * from student";
- $a = query_memcache($sql);
- //var_dump($a);
- function query_memcache($sql,$type=''){
- global $mem;
- global $db;
- $key = md5($sql);
- if(!($value = $mem->get($key))){ //Cache 中没有, 则从 MySQL 中查询
- $result = $db->query($sql);
- while($reattr = $result->fetch_assoc()){
- $attr[] = $reattr;
- }
- $value = $attr;
- // 将 Key 和 Value 写入 MemCache
- $mem->set($key,$value,0,time()+30);//set(key,value, 过期时间 / 单位秒);
- }
- return $value;
- }
- ?>
来源: http://www.bubuko.com/infodetail-2693422.html