写一个php memcache 简单的函数

发布时间:2020-08-08 13:02:32 作者:brucetam322
来源:网络 阅读:261


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

本次使用环境为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);
}
推荐阅读:
  1. linux下安装php扩展memcache
  2. linux下怎么安装php扩展

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 系统优化 缓存

上一篇:苹果证书(.p12)和描述文件(.mobileprovision)的快速申请

下一篇:甲骨文为推进AJAX和Java技术 广开开源之门(转)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》