Lachim / redis

Automatically exported from code.google.com/p/redis
2 stars 0 forks source link

[Feature Request] INCRTO DECRTO for strings, hashes, and sets #458

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I'm working on a rather complex highscore-system in redis, and have been 
thinking about a few simple additional commands that would simplify our system 
(and hopefully many others') alot.

When dealing with highscores, leaderboards, and possibly a lot of other 
scenarios we often want to change the value of a string, a hash-field or a 
score (sorted sets), BUT only if the existing value is smaller/bigger.

I propose to implement the following commands:

INCRTO key value
--------------------
Increments the number stored at key to value
if the existing number is more than than value.

Integer reply: the value of key after the operation.

DECRTO key value
----------------
Decrements the number stored at key to value
if the existing number is less than than value.

Integer reply: the value of key after the operation.

HINCRTO key field value
---------------------------
Increments the number stored at field in the hash stored at key to value
if the existing number is less than than value.

Integer reply: the value at field after the operation.

HDECRTO key field value
---------------------------
Decrements the number stored at field in the hash stored at key to value
if the existing number is greater than than value.

Integer reply: the value at field after the operation.

ZINCRTO key increment score
---------------------------
Increments the score of a member in a sorted set to score
if the existing number is less than than score.

Integer reply: the value at field after the operation.

ZDECRTO key decrement score
---------------------------
Decrements the score of a member in a sorted set to score
if the existing number is greater than than score.

Integer reply: the value at field after the operation.

Why?

When updating the score of a member in a sorted set. 
Now we always need to fetch the existing score first,
compare the two, and then optionally update it. In
the meantime the score could have been incremented
by another process, possibly to a higher score, which
results in us overwriting the higher score.

We meet the exact same challenges whenever we want
to do similar operations on numbers in keys and hashes
as well. The commands above would really clean up alot
and make it impossible for us to overwrite values that
should not be overwritten, and spares us the hazzle of
always fetching a number right before we update the value.

I can gladly write the docs (description, examples, etc)
if you'd consider implementing this.

And by the way, redis is seriously the most awesome piece
of technology I've used in the last few years. I love that
it is so clean, lean, and simple, and I truly think that
these suggested commands would not alter that concept at all :)

Best, Sindre

Original issue reported on code.google.com by habit...@gmail.com on 16 Feb 2011 at 12:10

GoogleCodeExporter commented 8 years ago
Ok, typo.. for the sorted set commands I meant:

ZINCRTO key score member

ZDECRTO key score member

Original comment by habit...@gmail.com on 16 Feb 2011 at 12:13