首页 > 后端开发 > php教程 > PHP 使用memcached

PHP 使用memcached

WBOY
发布: 2016-08-08 09:32:59
原创
1000 人浏览过

1.添加扩展包

    php_memcache.dll

2.在PHP.INI添加

    extension=php_memcache.dll

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.添加数组
    //在添加数组是,根据需要. 希望序列号放入  ,
    //serializeunserialize, 如果根据需要,也可以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>
登录后复制

以上就介绍了PHP 使用memcached,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板