memcache 常用方法

<?php
    $memcache = new Memcache;  //initialised memcahe
    $memcache->connect("127.0.0.1",11211);  // connect memcahe server

    //字符串
    $memcache->add("name","hometown");//
    $memcache->set("age",18);
    echo "name:".$memcache->get("name")."<br>age:".$memcache->get("age");
     echo "<br>";

    //数组
    $memcache->add("arr",array("hometown","18","male"));
    print_r($memcache->get("arr"));
    echo "<br>";

    //对象
    class person{
     private $name="hometown";
     private $age="19";
     var $sex="male";
    }
    $p=new person();
    $memcache->set("people",$p);
    print_r($memcache->get("people"));
     echo "<br>";

     //数据库
     $sql="SELECT * FROM dbjb.table_one";
     $key=md5($sql);
     $datas=$memcache->get($key);
     if(!$datas){         
         $mysqli=new mysqli("localhost","root","hometown1986","dbjb");
         $result=$mysqli->query($sql);

         $datas=array();
         while($row=$result->fetch_assoc())
         {
            $datas[]=$row;
         } 
         $result->free();
         $mysqli->close();

        $memcache->set("datas",$datas);
        //echo $sql;
     }

     echo "<pre>";
     print_r($datas);
     echo "</pre>"; 

     $memcache->close();
// $cache = getCache(md5("mysql_query" . $sql));
 //get sql query if already cached in memcahe

?>

基本用法如上,其他方法可以参考手册进行,

原文地址:https://www.cnblogs.com/hometown/p/6941135.html