/usr/share/pyshared/beaker/ext/memcached.py is in python-beaker 1.6.3-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | from __future__ import with_statement
from beaker.container import NamespaceManager, Container
from beaker.exceptions import InvalidCacheBackendError, MissingCacheParameter
from beaker.synchronization import file_synchronizer
from beaker.util import verify_directory, SyncDict
import warnings
_client_libs = {}
def _load_client(name='auto'):
if name in _client_libs:
return _client_libs[name]
def _pylibmc():
global pylibmc
import pylibmc
return pylibmc
def _cmemcache():
global cmemcache
import cmemcache
warnings.warn("cmemcache is known to have serious "
"concurrency issues; consider using 'memcache' "
"or 'pylibmc'")
return cmemcache
def _memcache():
global memcache
import memcache
return memcache
def _auto():
for _client in (_pylibmc, _cmemcache, _memcache):
try:
return _client()
except ImportError:
pass
else:
raise InvalidCacheBackendError(
"Memcached cache backend requires one "
"of: 'pylibmc' or 'memcache' to be installed.")
clients = {
'pylibmc': _pylibmc,
'cmemcache': _cmemcache,
'memcache': _memcache,
'auto': _auto
}
_client_libs[name] = clib = clients[name]()
return clib
class MemcachedNamespaceManager(NamespaceManager):
"""Provides the :class:`.NamespaceManager` API over a memcache client library."""
clients = SyncDict()
def __new__(cls, *args, **kw):
memcache_module = kw.pop('memcache_module', 'auto')
memcache_client = _load_client(memcache_module)
if memcache_module == 'pylibmc' or \
memcache_client.__name__.startswith('pylibmc'):
return object.__new__(PyLibMCNamespaceManager)
else:
return object.__new__(MemcachedNamespaceManager)
def __init__(self, namespace, url,
memcache_module='auto',
data_dir=None, lock_dir=None,
**kw):
NamespaceManager.__init__(self, namespace)
_memcache_module = _client_libs[memcache_module]
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
self.mc = MemcachedNamespaceManager.clients.get(
(memcache_module, url),
_memcache_module.Client,
url.split(';'))
def get_creation_lock(self, key):
return file_synchronizer(
identifier="memcachedcontainer/funclock/%s/%s" %
(self.namespace, key), lock_dir=self.lock_dir)
def _format_key(self, key):
return self.namespace + '_' + key.replace(' ', '\302\267')
def __getitem__(self, key):
return self.mc.get(self._format_key(key))
def __contains__(self, key):
value = self.mc.get(self._format_key(key))
return value is not None
def has_key(self, key):
return key in self
def set_value(self, key, value, expiretime=None):
if expiretime:
self.mc.set(self._format_key(key), value, time=expiretime)
else:
self.mc.set(self._format_key(key), value)
def __setitem__(self, key, value):
self.set_value(key, value)
def __delitem__(self, key):
self.mc.delete(self._format_key(key))
def do_remove(self):
self.mc.flush_all()
def keys(self):
raise NotImplementedError(
"Memcache caching does not "
"support iteration of all cache keys")
class PyLibMCNamespaceManager(MemcachedNamespaceManager):
"""Provide thread-local support for pylibmc."""
def __init__(self, *arg, **kw):
super(PyLibMCNamespaceManager, self).__init__(*arg, **kw)
self.pool = pylibmc.ThreadMappedPool(self.mc)
def __getitem__(self, key):
with self.pool.reserve() as mc:
return mc.get(self._format_key(key))
def __contains__(self, key):
with self.pool.reserve() as mc:
value = mc.get(self._format_key(key))
return value is not None
def has_key(self, key):
return key in self
def set_value(self, key, value, expiretime=None):
with self.pool.reserve() as mc:
if expiretime:
mc.set(self._format_key(key), value, time=expiretime)
else:
mc.set(self._format_key(key), value)
def __setitem__(self, key, value):
self.set_value(key, value)
def __delitem__(self, key):
with self.pool.reserve() as mc:
mc.delete(self._format_key(key))
def do_remove(self):
with self.pool.reserve() as mc:
mc.flush_all()
class MemcachedContainer(Container):
"""Container class which invokes :class:`.MemcacheNamespaceManager`."""
namespace_class = MemcachedNamespaceManager
|