user1950164
user1950164

Reputation:

Redis and redis-py: Storing abstract objects

In Python I have objects that contain other objects. What is the best way to represent this using Redis?

This answer adresses this. The solution is basically that you give every object an id and if a objectA contains objectB what you store in objectA is the id of objectB. If there's nothing better, I guess this seems reasonable.

Now my question is, how do I generate these ids? Lets say that my objects are users that contain other objects called items. Each unique item I give a unique id. But when a new item is created, how to I make sure that the id I give the new item doesn't exist already, without having to check the all the existing ids? Suppoose for example that I'm storying all the existing items in the redis namespace as item:int, item:5313, item:1234 etc. I want to create a new item, how do I check the existing ids in a way that's efficient?

Thanks.

Upvotes: 0

Views: 146

Answers (1)

Vitja Makarov
Vitja Makarov

Reputation: 76

You can use autoincrement id counter, like this:

redis 127.0.0.1:6379> incr next_id:user
(integer) 1
redis 127.0.0.1:6379> incr next_id:user
(integer) 2

Upvotes: 1

Related Questions