Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zAdd GT option seems to be ignored #2319

Closed
2 tasks done
taylorotwell opened this issue Jan 18, 2023 · 8 comments
Closed
2 tasks done

zAdd GT option seems to be ignored #2319

taylorotwell opened this issue Jan 18, 2023 · 8 comments

Comments

@taylorotwell
Copy link

taylorotwell commented Jan 18, 2023

Expected behaviour

When attempting to update the score of a member with the GT flag, the score should not be updated unless the score is greater than the existing score.

Actual behaviour

The score is updated when it should not be, even though 0 is returned by phpredis indicating no records should have been updated.

I'm seeing this behaviour on

  • OS: Mac / Linux
  • Redis: 7.0
  • PHP: 8.2
  • phpredis: 5.3.7

Steps to reproduce, backtrace or example script

$redis->zadd('example-key', 100, 'example-member');
$redis->zadd('example-key', ['GT'], 1, 'example-member');

I've checked

  • There is no similar issue from other users
  • Issue isn't fixed in develop branch
@oprypkhantc
Copy link

Reproducible on different OS, Redis, PHP and phpredis versions too.

@michael-grunder
Copy link
Member

michael-grunder commented Jan 18, 2023

The GT option isn't implemented in PhpRedis 5.3.7 but should be in the develop branch.

This works for me:

<?php

$r = new Redis;

$r->connect('localhost', 6379);
$r->del('zset');

// Adds member
var_dump($r->zAdd('zset', ['GT'], 4, 'bar'));

// Doesn't update
var_dump($r->zAdd('zset', ['GT'], 2, 'bar'));

var_dump($r->zRange('zset', 0, -1, true));

Edit:

The score is updated when it should not be, even though 0 is returned by phpredis indicating no records should have been updated

The return value confused me at first too, so I looked it up. Redis will return 0 even if the score was updated as it's returning the number of elements added. If you also specify the CH operator it will return the number of elements changed.

When used without optional arguments, the number of elements added to the sorted set (excluding score updates).

Example:

<?php

$r = new Redis;

$r->connect('localhost', 6379);
$r->del('zset');

var_dump($r->zAdd('zset', 1, 'bar'));

// Update, returns 0
var_dump($r->zAdd('zset', ['GT'], 3, 'bar'));

// Update, returns 1
var_dump($r->zAdd('zset', ['GT', 'CH'], 7, 'bar'));

@tillkruss
Copy link
Member

@michael-grunder Do we support GT in Relay already?

@michael-grunder
Copy link
Member

Yes.

@tillkruss
Copy link
Member

@taylorotwell: If you don't want to wait for a release, or worse compile phpredis from source, you can use Relay, which is identical to PhpRedis and it's got a Laravel integration: https://github.com/cachewerk/relay/tree/main/src/Laravel

@ixqbar
Copy link

ixqbar commented Jan 29, 2023

next release time ?

@yatsukhnenko
Copy link
Member

unknown

@tillkruss
Copy link
Member

tillkruss commented Mar 2, 2023

@taylorotwell: You can use rawCommand() if you need GT right now in PhpRedis:

$redis->zadd('example-key', 100, 'example-member');
$redis->rawCommand('ZADD', 'example-key', 'GT', 1, 'example-member');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants