写一个phpmemcache简单的函数

在一个项目中添加了memcache层,但由于数据库本来压力就不大,数据量很小,所以性能改善不是特别明显,因此学习并应用下来记录一下方便以后自己使用。这里我只应用了直接调用对应api函数的方法,另外一种方法是创建对象来连接memcache,具体是$mem=new Memcache,然后再调用对象里的方法来操作要存储的item。

创新互联公司专注于企业全网整合营销推广、网站重做改版、彭州网站定制设计、自适应品牌网站建设、HTML5建站购物商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为彭州等各大城市提供网站开发制作服务。

本次使用环境为php 5.4.17,

yum安装的驱动:

php-pecl-memcache-3.0.8-1.el5.remi

代码如下:

$MEMCACHE["host"]="10.54.178.202";
$MEMCACHE["port"]="11211";
$MEMCACHE["timeout"]="5";


function cache_set($key, $value, $expire = 86400, $flag = MEMCACHE_COMPRESSED, $cache_host = NULL) {
    if(empty($cache_host)) {
        global $MEMCACHE;
        $cache_host = $MEMCACHE["host"];
        $cache_port = $MEMCACHE["port"];
        $cache_timeout = $MEMCACHE["timeout"];
    }
    $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout);
    $memcache->set($key, $value, $flag, $expire);
    memcache_close($memcache);
}

function cache_get($key, $cache_host = null) {
    if(empty($cache_host)) {
        global $MEMCACHE;
        $cache_host = $MEMCACHE["host"];
        $cache_port = $MEMCACHE["port"];
        $cache_timeout = $MEMCACHE["timeout"];
    }

    $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout);
    $result=$memcache->get($key);
    memcache_close($memcache);
    return $result;
}

function cache_clear($key, $cache_host = null) {
    if(empty($cache_host)) {
        global $MEMCACHE;
        $cache_host = $MEMCACHE["host"];
        $cache_port = $MEMCACHE["port"];
        $cache_timeout = $MEMCACHE["timeout"];
    }
    $memcache = memcache_connect($cache_host,$cache_port,$cache_timeout);
    $memcache->delete($key, 0);
    memcache_close($memcache);
}

网页标题:写一个phpmemcache简单的函数
链接地址:https://www.cdcxhl.com/article0/jshiio.html

成都网站建设公司_创新互联,为您提供外贸建站App开发网站营销小程序开发微信公众号手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司