Redis“未知命令”错误

时间:2016-04-24 19:02:35

标签: php laravel laravel-4 redis predis

我在我的网站(Laravel 4.2)中使用Redis作为会话存储。有时我会遇到以下错误。我想“>” char打破了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;
    }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

我建议更新问题并提及您使用的redis客户端模块。它是先天吗?

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

使用telnet重现

执行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的一部分。看起来您正在将HTML数据存储在redis中。我只能猜测客户端不会计算strlen()count()并在“telnet行”下发送错误信息。

相关问题