redis-dict /

    import cPickle as pickle
except ImportError:
    import pickle

class RedisDict(object):

    def __init__(self, name, connection, serializer=pickle): = name
        self.serializer = serializer
        self._redis = connection

    def __len__(self):
        return self._redis.hlen(

    def _deserialize(self, msg):
        if msg is not None and self.serializer is not None:
            msg = self.serializer.loads(msg)
        return msg

    def _serialize(self, value):
        if self.serializer is not None:
            return self.serializer.dumps(value)
        return value

    def __contains__(self, key):
        assert isinstance(key, str)
        return self._redis.hexists(, key)

    def __getitem__(self, key):
        assert isinstance(key, str)
        if key in self:
            msg = self._redis.hget(, key)
            return self._deserialize(msg)
        raise KeyError(key)

    def __setitem__(self, key, msg):
        assert isinstance(key, str)
        msg_ = self._serialize(msg)
        self._redis.hset(, key, msg_)
        return msg

    def __delitem__(self, key):
        assert isinstance(key, str)
        self._redis.hdel(, key)

    def execute_update(self, key, updater):
        with self._redis.pipeline() as pipe:
            old_value = self._deserialize(pipe.hget(, key))
            new_value = updater(old_value)
            pipe.hset(, key, self._serialize(new_value))

    def get(self, key, default=None):
        return self[key] if key in self else default

    def clear(self):
        keys = self._redis.hkeys(
        if keys:
            self._redis.hdel(, *keys)

    def values(self):
        return [self._deserialize(v) for v in self._redis.hvals(]

    def keys(self):
        return self._redis.hkeys(