微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Thinkphp5.1里面使用Memcached的bug

在tp5.1里面使用缓存数据库memcached,我安装了memcached拓展。填好配置之后报以下错误Call to undefined method Memcached::has()。

查看代码发现tp5.1里面使用了has这个函数

    protected function setTagItem($name)
    {
        if ($this->tag) {
            $tagName = $this->getTagKey($this->tag);

            if ($this->handler->has($tagName)) {
                $this->handler->append($tagName, ',' . $name);
            } else {
                $this->handler->set($tagName, $name);
            }

            $this->tag = null;
        }
    }

查了下memcached文档,压根就没有has()这个函数,这个是定义的函数.故改成

    protected function setTagItem($name)
    {
        if ($this->tag) {
            $tagName = $this->getTagKey($this->tag);

            if ($this->has($tagName)) {
                $this->handler->append($tagName, ',' . $name);
            } else {
                $this->handler->set($tagName, $name);
            }

            $this->tag = null;
        }
    }

一共有两处地方,修改完成之后就不报未定义has函数错误了。等我以为完事了,程序又报另一个错误Memcached::append(): cannot append/prepend with compression turned on。查文档得知

在这里插入图片描述


这应该是memcached里面的一个bug,于是把压缩功能关闭。在构造函数里面添加以下代码

 //关闭压缩功能
 $this->handler->setoption(\Memcached::OPT_COMPRESSION, false);

至此,问题全部解决。我看了下memcache.PHP代码,跟memcached.PHP有点不一样。我猜测如果使用memcache拓展则不会产生以上的报错。
这里引申一个问题,memcache和memcached有什么区别?
网上很多官方的答案,概括起来就是

  • 实现了memcached接口的PHP扩展memcache,在PHP框架之内实现的;
  • 实现了memcached接口的PHP扩展memcached,基于libmemcached实现
  • memcached比memcache的功能要强。至于使用那个?我觉得用memcache靠谱一点,至少不会有这么多bug。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐