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

Source Code for Module memtools.storages.memcache

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  #       memcache.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   
 35  from threading import Lock, Thread 
 36  from memcache import Client as MemcacheClient 
 37  import logging 
 38  from memtools.protocols import Memory, MemoryPool 
 39  from memtools.storages import NotSet, OutOfBounds 
 40   
 41   
42 -class MemcacheMemory(Memory):
43 """ 44 Memory gateway to a Memcache server 45 """ 46
47 - def __init__(self, servers=["127.0.0.1:11211"], expire=0, debug=False):
48 """ 49 :param servers: List of servers to use. Please, read 50 memcache.Client help. 51 """ 52 self._client = MemcacheClient(servers) 53 self._expire = expire 54 logging.basicConfig(level=logging.WARNING) 55 self.log = logging.getLogger("Memcache-Gateway") 56 if debug: 57 self.log.setLevel(logging.DEBUG)
58
59 - def __getitem__(self, key):
60 self.log.debug("Accessing key %s", key) 61 value = self._client.get(key) 62 if isinstance(value, NotSet): 63 return None 64 elif value is None: 65 raise KeyError 66 else: 67 return value
68
69 - def __setitem__(self, key, value):
70 self.log.debug("Setting key") 71 if value is None: 72 value = NotSet() 73 self._client.set(key, value, self._expire)
74
75 - def __delitem__(self, key):
76 self.log.debug("Deleting key %s", key) 77 if self._client.delete(key) == 0: 78 raise KeyError
79 80
81 -class MemcacheMemoryPool(MemoryPool):
82
83 - def __init__(self, servers=["127.0.0.1:11211"], expire=0, upper_limit=100, 84 lower_limit=1, debug=False):
85 super(MemcacheMemoryPool, self).__init__() 86 self._clients = [MemcacheMemory(servers=servers, expire=expire) for o 87 in xrange(0, abs(upper_limit + lower_limit) / 2)] 88 self.__expire = expire 89 self._servers = servers 90 self.upper_limit = upper_limit 91 self.lower_limit = lower_limit 92 logging.basicConfig(level=logging.WARNING) 93 self.log = logging.getLogger("Memcache Pool") 94 self.__debug = debug 95 self.__clients_lock = Lock() 96 if debug: 97 self.log.setLevel(logging.DEBUG)
98
99 - def __expire_get(self):
100 return self.__expire
101
102 - def __expire_set(self, value):
103 if self.__expire != value: 104 self.__expire = value 105 for i in self._clients: 106 i._expire = value
107 108 _expire = property(__expire_get, __expire_set) 109
110 - def count(self):
111 try: 112 self.__clients_lock.acquire() 113 return len(self._clients) 114 finally: 115 self.__clients_lock.release()
116
117 - def grow(self, number=1):
118 self.log.debug("Adding %s new servers to the pool", number) 119 self.__clients_lock.acquire() 120 for i in range(number): 121 self._clients.append(MemcacheMemory(self._servers, self._expire, 122 self.__debug)) 123 self.__clients_lock.release()
124
125 - def shrink(self, number=1):
126 self.log.debug("Deleting %s servers from the pool", number) 127 self.__clients_lock.acquire() 128 for i in range(number): 129 self._clients.pop() 130 self.__clients_lock.release()
131
132 - def _claim_client(self):
133 if self.count() < self.lower_limit: 134 self.grow() 135 try: 136 self.__clients_lock.acquire() 137 return self._clients.pop() 138 finally: 139 self.__clients_lock.release()
140
141 - def _return_client(self, client):
142 if self.count() < self.upper_limit: 143 self.__clients_lock.acquire() 144 self._clients.append(client) 145 self.__clients_lock.release()
146
147 - def __getitem__(self, key):
148 self.log.debug("Accessing key %s", key) 149 try: 150 client = self._claim_client() 151 return client[key] 152 finally: 153 self.__return_client(client)
154
155 - def __setitem__(self, key, value):
156 self.log.debug("Setting key %s to %s", key, value) 157 try: 158 client = self._claim_client() 159 client[key] = value 160 finally: 161 self._return_client(client)
162
163 - def __delitem__(self, key):
164 self.log.debug("Deleting key %s", key) 165 try: 166 client = self._claim_client() 167 client.__delitem__(key) 168 finally: 169 self._return_client(client)
170