1. 程式人生 > >php memcache緩存

php memcache緩存

memcach con code memcache spa dump 緩存 ret reat

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

php memcache緩存