I am storing strings and numbers in Redis.
I use basic key value stores to keep track of the current color value.
DATA:color:ID1 = red
DATA:color:ID2 = red
DATA:color:ID3 = red
DATA:color:ID4 = blue
I use SETs to allow for quick lookup of colors and corresponding IDs.
INDEX:color:red = {ID1, ID2, ID3}
INDEX:color:blue = {ID4}
How should I go about updating these values? If ID1 becomes blue, we will need to run the following commands:
SADD INDEX:color:blue ID1
SREM INDEX:color:red ID1
SET DATA:color:ID1 blue
The issue though is I do not know which SET ID1 is inside at the time of updating the value to blue. I can achieve this using a lua script, but this violates the rules about generating dynamic keys. The lua script would look like:
-- ARGV[1] is the index key
-- ARGV[2] is the ID
-- ARGV[3] is the new value
local k = redis.call('GET', KEYS[1])
redis.call('SREM', ARGV[1] .. ":" .. k, ARGV[2])
redis.call('SADD', ARGV[1] .. ":" .. ARGV[3], ARGV[2])
redis.call('SET', KEYS[1], ARGV[3])
Example usage would look like:
EVALSHA <SHA> 1 DATA:color:ID1 INDEX:color ID1 blue
If I were storing numbers, this is trivial as I can use sorted sets.
Is there a way to achieve this without violating the guidelines?