tp框架下执行更新操作时,ERR wrong number of arguments for 'del' command错误

376 阅读1分钟
$info = (new Test)->where(['id' => $id])->find();
$info->name = $name;
$info->save();

示例代码如上

tp框架中,使用predis时,会出现ERR wrong number of arguments for 'del' command问题

情况1:未安装redis扩展,并且设置默认缓存为redis形式,Redis类加载\Predis\Client

'default' => env('cache.driver', 'redis'),
namespace think\cache\driver;

use think\cache\Driver;

class Redis extends Driver
{
public function __construct(array $options = [])
{
    if (!empty($options)) {
        $this->options = array_merge($this->options, $options);
    }

    if (extension_loaded('redis')) {
        $this->handler = new \Redis;

        if ($this->options['persistent']) {
            $this->handler->pconnect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']);
        } else {
            $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
        }

        if ('' != $this->options['password']) {
            $this->handler->auth($this->options['password']);
        }
    } elseif (class_exists('\Predis\Client')) {
        $params = [];
        foreach ($this->options as $key => $val) {
            if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
                $params[$key] = $val;
                unset($this->options[$key]);
            }
        }

        if ('' == $this->options['password']) {
            unset($this->options['password']);
        }

        $this->handler = new \Predis\Client($this->options, $params);

        $this->options['prefix'] = '';
    } else {
        throw new \BadFunctionCallException('not support: redis');
    }

    if (0 != $this->options['select']) {
        $this->handler->select((int) $this->options['select']);
    }
}

}

情况2:指定了使用predis方式

问题定位:在执行save时,删除标签操作未判断为空的情况,导致predis报错


namespace think\cache;

/**
 * 标签集合
 */
class TagSet
{
    /**
     * 清除缓存
     * @access public
     * @return bool
     */
    public function clear(): bool
    {
        // 指定标签清除
        foreach ($this->tag as $tag) {
            $names = $this->handler->getTagItems($tag);
            $this->handler->clearTag($names);

            $key = $this->handler->getTagKey($tag);
            $this->handler->delete($key);
        }

        return true;
    }

}

添加为空判断即可解决

    public function clear(): bool
    {
        // 指定标签清除
        foreach ($this->tag as $tag) {
            $names = $this->handler->getTagItems($tag);
            if (!empty($names)) $this->handler->clearTag($names);

            $key = $this->handler->getTagKey($tag);
            $this->handler->delete($key);
        }

        return true;
    }