Reputation: 2597
I'm trying to implement the following logic in my go program:
def action:
1. GET the value of KEY from Redis
2. SET the value of KEY to 0
I'm using go-redis
module to interact with my Redis instance.
I'm familiar with Redis transactions but I couldn't seem to find a decent document or example on how to implement the desired set of actions in a concurrent-safe and atomic manner in Golang.
Question: How can I implement the defined action
and be sure no other Redis operation will take place between steps 1 and 2 even if I access my Redis instance from different go routines?
Upvotes: 0
Views: 1225
Reputation: 49092
You can accomplish this by using the single Redis command GETSET
, which will be atomic:
GETSET KEY 0
More broadly, there's nothing Go-specific about ensuring atomicity in Redis. You just use the same Redis transaction facilities (via the Go Redis client) that you're familiar with.
Upvotes: 1