tags:

views:

70

answers:

2

So im working with redis in a python app. any advice on key management? i try and keep all redis calls in one location but something seems off with hardcoding keys everywhere. tips?

A: 

Well, key names are comparable to classes and attributes in your application, so some degree of 'repetition' will happen.

However, if you find you're repeating a lot of code to actually build your keys, then you might want to look at a mapper. In Python there's Redisco.

Or if you need something simpler, in Ruby there's Nest (porting it to Python should be trivial). It allows you to DRY up your references to keys:

users = Nest.new("User")

user = users[1]
# => "User:1"

user[:name]
# => "User:1:name"

redis.set(user[:name], "Foo")

# or even:
user[:name].set("Foo")

user[:name].get
# => "Foo"
djanowski
A: 

I use a set of wrapper classes that handle key generation, something like:

public User Get(string username) {
    return redis.Get("user:"+username);
}

I have an instance of each of these classes globally available, so just need to call

Server.Users.Get(username);

That's in .NET of course, but something similar should work in any language. An additional advantage of this approach over using a generic mapping tool is that it provides a good place to put things like connection sharing and indexing.

Tom Clarkson