• 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
    
    ?>

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

  • 相关阅读:
    python-django学习
    c++异常处理
    Python输入输出
    Python变量
    Python异常处理
    Python起源与发展
    vsftpd基于mysql的认证方式
    vsftpd搭建ftp服务,并实现虚拟用户访问
    httpd结合php的fpm模式
    编译安装apache
  • 原文地址:https://www.cnblogs.com/hometown/p/6941135.html
Copyright © 2020-2023  润新知