baao
baao

Reputation: 73251

Set options for ZADD command in laravel redis

I'm trying to set options for ZADD with laravel redis but am failing.

The option I need to set is NX, as stated in the documentation:

ZADD options (Redis 3.0.2 or greater)

ZADD supports a list of options, specified after the name of the key and before the first score argument.

So I wrote it like this:

$this->redis->zAdd('orderIDs:' . $category, 'NX',[$orderId => $timestamp[1]]);

The error message I get is

PHP warning: strlen() expects parameter 1 to be string, array given in /RediMail/vendor/predis/predis/src/Connection/StreamConnection.php on line 270

I also tried to use put 'NX' to other positions, but laravel doesn't seem to like the idea of using options for ZADD.

Is there a way to do this with laravel or will I need to use another way of setting my sorted set with options?

I'm using Redis 3.0.2.

From predis/predis:

    class ZSetAdd extends Command
{
    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return 'ZADD';
    }
    /**
     * {@inheritdoc}
     */
    protected function filterArguments(array $arguments)
    {
        if (count($arguments) === 2 && is_array($arguments[1])) {
            $flattened = array($arguments[0]);
            foreach ($arguments[1] as $member => $score) {
                $flattened[] = $score;
                $flattened[] = $member;
            }
            return $flattened;
        }
        return $arguments;
    }
}

Doesn't look like predis is accepting options, or am I missing something?

Upvotes: 0

Views: 2375

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49972

Until Predis' zAdd method is updated to support the changes in Redis v3.0.2, your best bet is to explore the wonderful world of RawCommand: https://github.com/nrk/predis/blob/master/src/Command/RawCommand.php

It should let you construct your own commands, including the ZADD NX ... variant.

Upvotes: 2

Related Questions