2 This module contains the container classes to create objects
3 that persist directly in a Redis server.
7 from functools import partial
10 class Container(object):
11 """Create a container object saved in Redis.
14 key -- the Redis key this container is stored at
15 db -- the Redis client object. Default: None
17 When ``db`` is not set, the gets the default connection from
18 ``redisco.connection`` module.
21 def __init__(self, key, db=None, pipeline=None):
24 self.pipeline = pipeline
27 """Remove container from Redis database."""
30 def __getattribute__(self, att):
31 if att in object.__getattribute__(self, 'DELEGATEABLE_METHODS'):
32 return partial(getattr(object.__getattribute__(self, 'db'), att), self.key)
34 return object.__getattribute__(self, att)
42 if hasattr(self, 'db_cache') and self.db_cache:
45 from redisco import connection
46 self.db_cache = connection
49 DELEGATEABLE_METHODS = ()
52 class Hash(Container, collections.MutableMapping):
54 def __getitem__(self, att):
57 def __setitem__(self, att, val):
60 def __delitem__(self, att):
67 return self.hgetall().__iter__()
69 def __contains__(self, att):
70 return self.hexists(att)
73 return "<%s '%s' %s>" % (self.__class__.__name__, self.key, self.hgetall())
84 def _set_dict(self, new_dict):
88 dict = property(_get_dict, _set_dict)
90 DELEGATEABLE_METHODS = ('hlen', 'hset', 'hdel', 'hkeys', 'hgetall', 'hvals',
91 'hget', 'hexists', 'hincrby', 'hmget', 'hmset')