• php操作Memcache示例


    <?php
    //==============================实例化============================
    $mem=new Memcache;
    
    
    //==============================新建连接============================
    if(!$mem->connect("127.0.0.1",11211))
    {
    die("连接失败"); } //==============================增加============================ //1.增加一个字符串 if($mem->add("key1","abc",MEMCACHE_COMPRESSED,60)) { echo "ok"; } //2.增加一个数字 if($mem->add("key2",100,MEMCACHE_COMPRESSED,60)) { echo "ok"; } //3.增加一个数组 $arr=array("a"=>"字符串","b"=>"插入"); if($mem->add("key3",$arr,MEMCACHE_COMPRESSED,60)) { echo "ok"; }
    //4.增加一个对象 $dog1=new Dog("小狗",50); if($mem->add("key4",$dog1,MEMCACHE_COMPRESSED,60)) { echo "ok"; } //5.添加null 布尔值(bool类型的多种 没有元素的数组) if($mem->add("key5",null,MEMCACHE_COMPRESSED,60)) { echo "ok"; } //6.资源类型 目前还不支持mysql资源 $con=mysql_connect("127.0.0.1","root","111111"); if(!$con) { die("连接不成功"); } if($mem->add("key6",$con,MEMCACHE_COMPRESSED,60)) { echo "ok"; } else { echo "加入资源型失败"; }
    //7.序列化 $arr2=array("a"=>"字符串","b"=>"插入"); if($mem->add("key7",serialize($arr2),MEMCACHE_COMPRESSED,60)) { echo "ok"; } //==============================查询============================ echo "<hr/>"; echo "key1的值:"; $val=$mem->get("key1"); var_dump($val); echo "<hr/>"; echo "key2的值:"; $val=$mem->get("key2"); var_dump($val); echo "<hr/>"; echo "key3的值:"; $val=$mem->get("key3"); var_dump($val); echo "<hr/>"; echo "key4的值:"; $val=$mem->get("key4"); var_dump($val); echo "<hr/>"; echo "key5的值:"; $val=$mem->get("key5"); var_dump($val); echo "<hr/>"; echo "key6的值:"; $val=$mem->get("key6"); var_dump($val); $val=$mem->get("key7"); echo "<hr/>"; echo "key7的值:"; var_dump("key7".$val); //==============================修改============================ if($mem->replace("key7","hello",MEMCACHE_COMPRESSED,60)){ echo "replace ok"; }else{ echo "replace no ok"; } //==============================删除============================ if($mem->delete("key14")){ echo "delete ok"; }else{ echo "not exsists"; } //==============================清空所有============================ //清空所有 $mem->flush(); //==============================关闭连接============================ $mem->close(); class Dog { public $name; public $age; public function __construct($name,$age) { $this->name=$name; $this->age=$age; } }
  • 相关阅读:
    Java方法命名之“由简入繁”原则
    设置Tomcat的字符编码
    MyBatis之会话Session原理
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fa5519] was not registered for synchronization because synchronization is not active
    python3 驱动 PyMySQL
    win 7 64 安装 MondgoDB 3.4
    Python
    win 7 64 安装 tensorflow
    俞敏洪:2017年上半年我的阅读书单和笔记 转
    阿里巴巴开源产品列表 转
  • 原文地址:https://www.cnblogs.com/lizhaoyao/p/4891362.html
Copyright © 2020-2023  润新知