Discussion:
[redis-db] Set max possible value for a key in redis
Astro
2018-11-14 07:03:09 UTC
Permalink
Currently, is there a way in redis to set max possible value of a key?

For example,

set maxvalue key1 100 -- sets the max possible value of key1 to 100

set key1 10 -> success
incr key1 -> success
set key1 99 -> success
incr key1 -> success
incr key1 -> fail (max value of key1 100 is reached)

This can be helpful in cases where concurrent clients are trying to access
same key in redis.

Any help will be appreciated.

Thanks!
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+***@googlegroups.com.
To post to this group, send email to redis-***@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.
Itamar Haber
2018-11-14 08:38:09 UTC
Permalink
Hello Astro

There's no built-in way to put constraints on the value of a key, such as a
maximum integer value for a string. However.

In order to deal with concurrent updates, you can use a WATCH/MULTI/EXEC
block (a transaction). Alternatively, a Lua script (the EVAL command) can
be used for even finer control (e.g. use a Hash where one field is the
current counter value and another is the maximal value).

Lastly, you may consider the BITFIELD and its OVERFLOW property - it can
provide a behavior similar to the one you propose, but maximal values are
powers of two.

Cheers,
Itamar
Post by Astro
Currently, is there a way in redis to set max possible value of a key?
For example,
set maxvalue key1 100 -- sets the max possible value of key1 to 100
set key1 10 -> success
incr key1 -> success
set key1 99 -> success
incr key1 -> success
incr key1 -> fail (max value of key1 100 is reached)
This can be helpful in cases where concurrent clients are trying to access
same key in redis.
Any help will be appreciated.
Thanks!
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+***@googlegroups.com.
To post to this group, send email to redis-***@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.
Loading...