3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
gprime
  • 2,283
  • 7
  • 38
  • 50

2 Answers2

4

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

gprime
  • 2,283
  • 7
  • 38
  • 50
1

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);
Colum
  • 3,844
  • 1
  • 22
  • 26
  • 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. – gprime Jul 21 '11 at 15:04