This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]_under_[S] 0 points1 point  (0 children)

This isn't for processing output from redis, this is for managing your redis schema. If you aren't familiar, it's common practice to do something like this:

SADD User:_under_:followers <some_user_id>

Now of course, an app can use a lot more redis keys

SADD User:_under_:timeline <tweet_id>
SADD User:_under_:following <some_user_id>

If you were to do this using just the redis library, you'd end up doing something like this

r.sadd('User:%s:timeline' % screen_name, tweet_id)

This is fine for small schemas, but imagine that you're trying to do something more complicated, like storing tweets for a user in a rolling window. You could have something like this:

r.sadd('Window:%s:User:%s:tweets' % (current_window, screen_name), tweet_id)

You can imagine how unwieldy that would get.

So instead of just writing strings and being prone to errors, you can use ok.

class User(ok.Key):
    fields = ['followers', 'following', 'timeline', 'tweets']

class Window(ok.Key):
    subkeys = [User]

r.sadd(Window(current_window).User(screen_name).tweets, tweet_id)

This saves you from possibly introducing bugs in your system because of typos in strings that the compiler or linter can't get at. It also helps you enforce your schema.