0

I'm very new to redis and I'm trying to do the following inside a tranaction:

increment key add incremented value from before to a set

using redis commands it would be something like

multi
i = incr "value"
sadd "set" i
exec

Is there any redis command that would do that in one step with out needing two transactions and handling the value from the code (python code in my case)?

Many thanks

2 Answers 2

3

Unfortunately you can't do that directly. You can use a MULTI/EXEC block to guarantee atomicity of the query in the following way:

do {
    WATCH value
    i = 1 + GET value
    MULTI
    set value i
    SADD set i
    result = EXEC
} while (result == nil)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! WATCH was the missing piece.
0

There is a GETSET (http://redis.io/commands/getset) command for Redis where you can get the "old" value of a variable and set it to a new on the same command.

Almost certain that there is no method to incr a value and add it to a set because they are different data types for redis.

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.