lwzm
6/25/2014 - 2:59 AM

redis_session.py

import redis

db = redis.StrictRedis()
hget = db.hget
hset = db.hset

class Session(object):
    def __init__(self, name):
        self.name = name
    def __getitem__(self, key):
        value = hget(self.name, key)
        return loads(value) if value is not None else value
    def __setitem__(self, key, value):
        hset(self.name, key, dumps(value))
    def __repr__(self):
        return "\n".join(
            "{}: {!r}".format(k.decode(), loads(v))
            for k, v in db.hgetall(self.name).items()
            )