php memcache缓存

<?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;
     }
?>

猜你喜欢

转载自www.cnblogs.com/sunhao1987/p/9343730.html