1 # Copyright (c) 2010 Tim Medina
3 # Permission is hereby granted, free of charge, to any person
4 # obtaining a copy of this software and associated documentation
5 # files (the "Software"), to deal in the Software without
6 # restriction, including without limitation the rights to use,
7 # copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 # OTHER DEALINGS IN THE SOFTWARE.
24 # The original code link is https://github.com/iamteem/redisco/tree/master/redisco/containers.py
26 This module contains the container classes to create objects
27 that persist directly in a Redis server.
30 from functools import partial
33 class Container(object):
34 """Create a container object saved in Redis.
37 key -- the Redis key this container is stored at
38 db -- the Redis client object. Default: None
40 When ``db`` is not set, the gets the default connection from
41 ``redisco.connection`` module.
44 def __init__(self, key, db=None, pipeline=None):
47 self.pipeline = pipeline
50 """Remove container from Redis database."""
53 def __getattribute__(self, att):
54 if att in object.__getattribute__(self, 'DELEGATEABLE_METHODS'):
55 return partial(getattr(object.__getattribute__(self, 'db'), att), self.key)
57 return object.__getattribute__(self, att)
65 if hasattr(self, 'db_cache') and self.db_cache:
68 from lcm.pub.redisco import connection
69 self.db_cache = connection
72 DELEGATEABLE_METHODS = ()
75 class Hash(Container, collections.MutableMapping):
77 def __getitem__(self, att):
80 def __setitem__(self, att, val):
83 def __delitem__(self, att):
90 return self.hgetall().__iter__()
92 def __contains__(self, att):
93 return self.hexists(att)
96 return "<%s '%s' %s>" % (self.__class__.__name__, self.key, self.hgetall())
105 return self.hgetall()
107 def _set_dict(self, new_dict):
109 self.update(new_dict)
111 dict = property(_get_dict, _set_dict)
113 DELEGATEABLE_METHODS = ('hlen', 'hset', 'hdel', 'hkeys', 'hgetall', 'hvals',
114 'hget', 'hexists', 'hincrby', 'hmget', 'hmset')