Package memtools :: Package storages :: Module redis
[hide private]

Source Code for Module memtools.storages.redis

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  #       redis.py 
 5  #        
 6  #       Copyright 2010 Pablo Alejandro Costesich <pcostesi@alu.itba.edu.ar> 
 7  #        
 8  #       Redistribution and use in source and binary forms, with or without 
 9  #       modification, are permitted provided that the following conditions are 
10  #       met: 
11  #        
12  #       * Redistributions of source code must retain the above copyright 
13  #         notice, this list of conditions and the following disclaimer. 
14  #       * Redistributions in binary form must reproduce the above 
15  #         copyright notice, this list of conditions and the following disclaimer 
16  #         in the documentation and/or other materials provided with the 
17  #         distribution. 
18  #       * Neither the name of the  nor the names of its 
19  #         contributors may be used to endorse or promote products derived from 
20  #         this software without specific prior written permission. 
21  #        
22  #       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23  #       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24  #       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25  #       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26  #       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27  #       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  #       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  #       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  #       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  #       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  #       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33   
34  from redis.client import Redis as RedisClient 
35  import logging 
36  from memtools.protocols import Memory, MemoryPool 
37  from memtools.storages import NotSet, OutOfBounds 
38   
39  try: 
40      from cPickle import dumps, loads 
41  except ImportError: 
42      from pickle import dumps, loads 
43   
44   
45 -class RedisMemory(Memory):
46 """ 47 Memory gateway to a Redis server 48 """ 49
50 - def __init__(self, expire=None, debug=False, 51 *args, **kwargs):
52 """ 53 :param servers: List of servers to use. Please, read 54 redis.Redis help. 55 56 """ 57 self._client = RedisClient(*args, **kwargs) 58 self._expire = expire 59 logging.basicConfig(level=logging.WARNING) 60 self.log = logging.getLogger("Redis-Gateway") 61 if debug: 62 self.log.setLevel(logging.DEBUG)
63
64 - def __getitem__(self, key):
65 self.log.debug("Accessing key %s", key) 66 value = self._client.get(key) 67 if value: 68 value = loads(str(value)) 69 if isinstance(value, NotSet): 70 return None 71 elif value is None: 72 raise KeyError 73 else: 74 self.log.debug("Key %s returned %s", key, value) 75 return value
76
77 - def __setitem__(self, key, value):
78 self.log.debug("Setting key %s to %s", key, value) 79 if value is None: 80 value = NotSet() 81 self._client.set(key, dumps(value)) 82 if self._expire: 83 self._client.expire(key, self._expire)
84
85 - def __delitem__(self, key):
86 self.log.debug("Deleting key %s", key) 87 if self._client.delete(key) == 0: 88 raise KeyError
89
90 - def expire(self, key, time):
91 self.log.debug("Setting expire time to %s seconds for key %s", 92 time, key) 93 self._client.expire(key, time)
94
95 - def __getattr__(self, attr):
96 redis_attr = getattr(self._client, attr) 97 return redis_attr
98