Peter
Peter

Reputation: 759

Redis : incr a value from a hget key

Firstly I'm new to Redis so please bear with me, as I'm sure this is a simple solution.

I'm trying to incr a value from within my hash. This is how I'm setting my key 'hset user:likes 2 10' so the first part is my key followed by another key, which is the users id number and then the value (10). What I need to do is incr the value of 10 to 11 but I'm not sure how to do this. I know I can use 'hget user:likes:total:count 2' to return the value but how do I then use the incr command as well as the hget command at the same time?

I'm using Ruby on Rails to implement this by the way using the redis gem.

Upvotes: 1

Views: 1003

Answers (1)

Tyson
Tyson

Reputation: 6244

You want HINCRBY:

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

I'm a little unclear on your key structure as described above. But basically, you pass in the name of the hash ("user:likes"), the hash key (in this case, I'm assuming a user id of 2), and the value by which you want to increment (in this case, 1).

Upvotes: 5

Related Questions