| 1 | ''' |
| 2 | Provides several CacheStore backends for Cheetah's caching framework. The |
| 3 | methods provided by these classes have the same semantics as those in the |
| 4 | python-memcached API, except for their return values: |
| 5 | |
| 6 | set(key, val, time=0) |
| 7 | set the value unconditionally |
| 8 | add(key, val, time=0) |
| 9 | set only if the server doesn't already have this key |
| 10 | replace(key, val, time=0) |
| 11 | set only if the server already have this key |
| 12 | get(key, val) |
| 13 | returns val or raises a KeyError |
| 14 | delete(key) |
| 15 | deletes or raises a KeyError |
| 16 | ''' |
| 17 | import time |
| 18 | |
| 19 | |
| 20 | class Error(Exception): |
| 21 | pass |
| 22 | |
| 23 | |
| 24 | class AbstractCacheStore(object): |
| 25 | |
| 26 | def set(self, key, val, time=None): |
| 27 | raise NotImplementedError |
| 28 | |
| 29 | def add(self, key, val, time=None): |
| 30 | raise NotImplementedError |
| 31 | |
| 32 | def replace(self, key, val, time=None): |
| 33 | raise NotImplementedError |
| 34 | |
| 35 | def delete(self, key): |
| 36 | raise NotImplementedError |
| 37 | |
| 38 | def get(self, key): |
| 39 | raise NotImplementedError |
| 40 | |
| 41 | |
| 42 | class MemoryCacheStore(AbstractCacheStore): |
| 43 | def __init__(self): |
| 44 | self._data = {} |
| 45 | |
| 46 | def set(self, key, val, time=0): |
| 47 | self._data[key] = (val, time) |
| 48 | |
| 49 | def add(self, key, val, time=0): |
| 50 | if key in self._data: |
| 51 | raise Error('a value for key %r is already in the cache' % key) |
| 52 | self._data[key] = (val, time) |
| 53 | |
| 54 | def replace(self, key, val, time=0): |
| 55 | if key in self._data: |
| 56 | raise Error('a value for key %r is already in the cache' % key) |
| 57 | self._data[key] = (val, time) |
| 58 | |
| 59 | def delete(self, key): |
| 60 | del self._data[key] |
| 61 | |
| 62 | def get(self, key): |
| 63 | (val, exptime) = self._data[key] |
| 64 | if exptime and time.time() > exptime: |
| 65 | del self._data[key] |
| 66 | raise KeyError(key) |
| 67 | else: |
| 68 | return val |
| 69 | |
| 70 | def clear(self): |
| 71 | self._data.clear() |
| 72 | |
| 73 | |
| 74 | class MemcachedCacheStore(AbstractCacheStore): |
| 75 | servers = ('127.0.0.1:11211') |
| 76 | |
| 77 | def __init__(self, servers=None, debug=False): |
| 78 | if servers is None: |
| 79 | servers = self.servers |
| 80 | from memcache import Client as MemcachedClient |
| 81 | self._client = MemcachedClient(servers, debug) |
| 82 | |
| 83 | def set(self, key, val, time=0): |
| 84 | self._client.set(key, val, time) |
| 85 | |
| 86 | def add(self, key, val, time=0): |
| 87 | res = self._client.add(key, val, time) |
| 88 | if not res: |
| 89 | raise Error('a value for key %r is already in the cache' % key) |
| 90 | self._data[key] = (val, time) |
| 91 | |
| 92 | def replace(self, key, val, time=0): |
| 93 | res = self._client.replace(key, val, time) |
| 94 | if not res: |
| 95 | raise Error('a value for key %r is already in the cache' % key) |
| 96 | self._data[key] = (val, time) |
| 97 | |
| 98 | def delete(self, key): |
| 99 | res = self._client.delete(key, time=0) |
| 100 | if not res: |
| 101 | raise KeyError(key) |
| 102 | |
| 103 | def get(self, key): |
| 104 | val = self._client.get(key) |
| 105 | if val is None: |
| 106 | raise KeyError(key) |
| 107 | else: |
| 108 | return val |
| 109 | |
| 110 | def clear(self): |
| 111 | self._client.flush_all() |