Home > php教程 > php手册 > body text

php使用memcached简单示例分享,phpmemcached示例

WBOY
Release: 2016-06-13 08:42:03
Original
1233 people have browsed it

php使用memcached简单示例分享,phpmemcached示例

本文章简单向码农们介绍一下memcached。memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速WEB动态应用程序。今天我和码农们先来简单探讨下memcached的用法。

1.添加memcached扩展包

<code class="hljs">php_memcache.dll</code>
Copy after login

2.在PHP.INI添加memcached扩展

<code class="language-php hljs">extension=php_memcache.dll</code>
Copy after login

3.程序

<?<span>php
    </span><span>//</span><span>创建一个mem对象实例</span>
    <span>$mem</span>=<span>new</span><span> Memcache;
    </span><span>if</span>(!<span>$mem</span>->connect("10.18.110.213",11211<span>)){
        </span><span>die</span>('连接失败!'<span>);
    }
    </span><span>//</span><span>增加
    //1.增加一个字串</span><span>
/*</span><span>    if($mem->set('key1',"beijing",MEMCACHE_COMPRESSED,60)){
        echo '添加ok';
    }</span><span>*/</span>
    <span>//</span><span>2.添加数值</span><span>
/*</span><span>    if($mem->set('key1',100,MEMCACHE_COMPRESSED,60)){
        echo '添加ok';
    }</span><span>*/</span>
    <span>//</span><span>3.添加数组
    //在添加数组是,根据需要. 希望序列号放入  ,
    //serialize<=>unserialize, 如果根据需要,也可以json_encode <=> json_decode</span>
    <span>$arr</span>=<span>array</span>("bj",'tj'<span>);
    </span><span>if</span>(<span>$mem</span>->set('key1',<span>$arr</span>,MEMCACHE_COMPRESSED,<span>time</span>()+31*3600*24<span>)){
        </span><span>echo</span> '添加数组ok99111'<span>;
    }
    </span><span>//</span><span>4.添加对象</span><span>
/*</span><span>    class Dog{
        public $name;
        public $age;
        public function __construct($name,$age){
            $this->name=$name;
            $this->age=$age;
        }
    }
    $dog1=new Dog('小狗',50);
    if($mem->set('key1',$dog1,MEMCACHE_COMPRESSED,60)){
        echo '添加对象ok';
    }</span><span>*/</span>
    <span>//</span><span>5.添加null 布尔值</span><span>
/*</span><span>    if($mem->set('key1',false,MEMCACHE_COMPRESSED,60)){
        echo '添加布尔ok';
    }</span><span>*/</span>
    <span>//</span><span>6. 资源类型放入.</span><span>
/*</span><span>    $con=mysql_connect("127.0.0.1","root","root");
    if(!$con){
        die('连接数据库失败');
    }
    var_dump($con);
    echo "<br/>";
    if($mem->set('key1',$con,MEMCACHE_COMPRESSED,60)){
        echo '添加资源ok';
    }</span><span>*/</span>
    <span>//</span><span>查询</span>
    <span>$val</span>=<span>$mem</span>->get('key1'<span>);
</span><span>//</span><span>修改
    //可以使用replace</span>
    <span>if</span>(<span>$mem</span>->replace("key11",'hello',MEMCACHE_COMPRESSED,60<span>)){
        </span><span>echo</span> 'replace ok'<span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> 'replace no ok'<span>;
    }
    </span><span>//</span><span>删除</span>
    <span>echo</span> "<br/>"<span>;
    </span><span>if</span>(<span>$mem</span>->delete('key14'<span>)){
        </span><span>echo</span> 'key14 删除'<span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> 'key14不存在'<span>;
    }</span>
Copy after login

原文地址:http://www.manongjc.com/article/683.html

相关阅读:

php扩展模块memcached长连接使用方法分析

php模块memcached使用指南

PHP5.5在windows安装使用memcached服务端的方法

php Memcached的使用方法介绍

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template