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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值