Question

I'm building a Rails web app with Redis. I'm trying to create key ids that would increase by one per key, with a type attached. Ex: HMSET user:2 name 'John' age '22'. Then the next one would be HMSET user:3 name 'Evan' age '29', and so on and so forth. I would have to set the user part of the key through the application. I'm wondering if there's a way to INCR the id (:2) part of each key, while ignoring the type. I don't want to do it through the application because I'll be running multiple instances of it through Unicorn or Puma, so there's a possibility that the same key could be written concurrently on different instances. Is this possible in Redis? Thanks in advance.

Était-ce utile?

La solution

Use a regular key to store the last id and INCR it with each new user. Take INCR's reply and use it as the 'id' part of your key name (with the HMSET command).

Even if you have multiple instances of your app, INCR-ing will ensure that you do not reuse ids.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top