stefano_cdn
stefano_cdn

Reputation: 1382

Rails Redis Reset Counters to 0

I would like to reset the value of a REDIS counter to 0 in a Rails 4 app.

I use hincrby to increment counters

 $redis.hincrby("user:likes", "key", 1)

I can't delete the key with hdel http://redis.io/commands/hdel because I need to get the key often.

GETSET is atomic and could do the job http://redis.io/commands/getset, as in the example

 GETSET mycounter "0"

But since I use hashes I need to use HSET http://redis.io/commands/hset

$redis.hset("user:likes", "key", "0")

It's not specified if hset is atomic, anyone used hset to reset redis counters to 0? If it's not a good option to reset a counter to 0, any idea how to do it?

Upvotes: 2

Views: 1212

Answers (1)

Vincent
Vincent

Reputation: 872

It is atomic, so if you run $redis.hset("user:likes", "key", "0"), it doesn't affect other fields inside the "user:likes" hash besides the field: "key"

Upvotes: 2

Related Questions