Predis Protocol error: invalid bulk length

10,856

Solution 1

I found the problem and a solution. Coming from memcached where it will serialize the array automatically this is not the same in PRedis. PRedis will never serialize anything when performing a set or get.

https://github.com/nrk/predis/issues/29

Solution 2

You have to use mset.

With the set command, Predis is looking for an array with only 2 variables (to set the key => hash). Do set 3 keys, you have to use mset.

To do what you seem to be trying to do:

$ids = array(1 => 'id-1', 2 => 'id-2', 3 => 'id-3');
$predis = new Predis\Client();

$predis->mset('testerKey', $ids);
Share:
10,856
gprime
Author by

gprime

Programming, Mathematics, Physics, Crossfit and Brazilian Jiu Jitsu

Updated on June 04, 2022

Comments

  • gprime
    gprime almost 2 years

    I am using predis and everything was great until I started getting this error:

    ERR Protocol error: invalid bulk length
    

    I am not sure why I am getting it. The error is in this file: Predis/Network/StreamConnection.php in this method:

    public function writeCommand(ICommand $command) {
        $commandId = $command->getId();
        $arguments = $command->getArguments();
    
        $cmdlen = strlen($commandId);
        $reqlen = count($arguments) + 1;
    
        $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
        for ($i = 0; $i < $reqlen - 1; $i++) {
            $argument = $arguments[$i];
            $arglen  = strlen($argument);
            $buffer .= "\${$arglen}\r\n{$argument}\r\n";
        }
        $this->writeBytes($buffer);
    }
    

    It fails when it tries to do an strlen() on an array.

    Here is the code that is causing this to fail:

    $ids = array(1, 2, 3);
    $predis = new Predis\Client();
    
    $predis->set('testerKey', $ids);
    

    Am I not allowed to set an array? Of course I can set an array. The only thing I changed was I make my files UTF-8 so maybe that screwed something up?

    Any help would be appreciated.

  • gprime
    gprime almost 13 years
    nope, I do not want to do a multi set. I want to store that array in a single key. It probably has something to do with encoding, I recently changed the encoding from the mac default to UTF-8.