Redis “未知命令”错误


Redis "unknown command" error

我在我的网站中使用Redis作为会话存储(Laravel 4.2)。有时我会收到以下错误。我猜">"字符破坏了setex命令。

production.ERROR: exception 'Predis'ServerException' with message 'ERR unknown command '>'' in 
production.ERROR: exception 'Predis'ServerException' with message 'ERR unknown command 'tml>''
production.ERROR: exception 'Predis'ServerException' with message 'ERR unknown command '</div>''

这些错误很少发生在生产服务器上,我无法重现它们。您知道为什么会发生这些错误以及如何防止它们吗?

key: laravel:xxxxxxxxxxxxxxx
value: s:217:"a:4:{s:6:"_token";s:40:"xxxxxxxxxxx";s:4:"lang";s:2:"fr";s:9:"_sf2_meta";a:3:{s:1:"u";i:1461777248;s:1:"c";i:1461777248;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}";
exception 'Predis'ServerException' with message 'ERR unknown command 'ml>'' in vendor/predis/predis/lib/Predis/Client.php:282

更新

我正在使用 redis 的代码示例。

    public function view($username = null)
    {
        $username = mb_strtolower($username);
        $redis = $this->getRedis();
        try{
            $view = $redis->get(User::getCacheKeyByUsername($username));
        }catch ('Exception $exception){
            $view = null;
        }
        if($view === null || Session::has("admin")){
            $user = User::where('username', '=', $username)->where("status", 1)->first();
            if (empty($user)) {
                return Redirect::to(Session::get("lang") . '/all');
            }
            $view = View::make("view", array("user" => $user));
            if(!Session::has("admin")){
                try{
                    $redis->setex(User::getCacheKeyByUsername($username), 3600, $view);
                }catch ('Exception $exception){
                    Log::error($exception->getMessage());
                }
            }
        }
        return $view;
    }

好的,所以基本上我可以从您的错误日志中说些什么:Redis 不喜欢像 <> 这样的特殊字符,所以你必须对它们进行编码。

检索时,使用 htmlspecialchars 进行编码,htmlspecialchars_decode解码数据。

您的 Redis 客户端有问题。您无法重现它,因为错误不会发生在您的代码中,而是发生在客户端和 redis 服务器之间的 TCP 通信中。

我建议更新问题并提及您正在使用的 Redis 客户端模块。是普雷迪斯吗?

如果你使用的是 unix,请尝试编译并安装 phpredis。 它是PHP模块,我从来没有遇到过任何问题。

<小时 />

使用远程登录进行复制

执行telnet host port并按照说明进行操作:

正常的请求GET a如下所示:

*2
$3
get
$1
a
$-1

现在考虑一下 - 要求gem a

*2
$3
gem
$1
a
-ERR unknown command 'gem'

协议有效,但命令"gem"无效。

现在考虑一下:

*1
$3
ml>
-ERR unknown command 'ml>'

这是你的错误。有效协议,无效命令。

或者考虑一下:

*2
$3
get
$1
<html>
$-1
-ERR unknown command 'ml>'

这又是你的错误。无效strlen("<html>");

为什么这是 redis 客户端错误而不是用户错误:

许多 Redis 客户端使用 PHP magic methods

这意味着如果你调用$redis->bla(),他们提取"bla"部分和它的参数,并将它们转发到redis服务器。

但是你不能做$redis->ml>()。这将是语法错误。所以这是来自 redis 客户端的错误,而不是来自您的代码的错误。

它看起来像HTML的一部分。看起来您正在 redis 中存储HTML数据。我只能猜测客户端不会计算strlen()count()并"沿着telnet线"发送错误的信息。