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。

猜你喜欢

转载自blog.csdn.net/lihuanlin/article/details/89398183