Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Attributes
MoinMoin.support.werkzeug.contrib.cache.MemcachedCache Class Reference
Inheritance diagram for MoinMoin.support.werkzeug.contrib.cache.MemcachedCache:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.werkzeug.contrib.cache.MemcachedCache:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def get
def get_dict
def add
def set
def get_many
def set_many
def delete
def delete_many
def clear
def inc
def dec

Public Attributes

 key_prefix
 default_timeout

Private Attributes

 _client

Detailed Description

A cache that uses memcached as backend.

The first argument can either be a list or tuple of server addresses
in which case Werkzeug tries to import the memcache module and connect
to it, or an object that resembles the API of a :class:`memcache.Client`.

Implementation notes:  This cache backend works around some limitations in
memcached to simplify the interface.  For example unicode keys are encoded
to utf-8 on the fly.  Methods such as :meth:`~BaseCache.get_dict` return
the keys in the same format as passed.  Furthermore all get methods
silently ignore key errors to not cause problems when untrusted user data
is passed to the get methods which is often the case in web applications.

:param servers: a list or tuple of server addresses or alternatively
                a :class:`memcache.Client` or a compatible client.
:param default_timeout: the default timeout that is used if no timeout is
                        specified on :meth:`~BaseCache.set`.
:param key_prefix: a prefix that is added before all keys.  This makes it
                   possible to use the same memcached server for different
                   applications.  Keep in mind that
                   :meth:`~BaseCache.clear` will also clear keys with a
                   different prefix.

Definition at line 251 of file cache.py.


Constructor & Destructor Documentation

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.__init__ (   self,
  servers,
  default_timeout = 300,
  key_prefix = None 
)

Definition at line 276 of file cache.py.

00276 
00277     def __init__(self, servers, default_timeout=300, key_prefix=None):
00278         BaseCache.__init__(self, default_timeout)
00279         if isinstance(servers, (list, tuple)):
00280             try:
00281                 import cmemcache as memcache
00282                 is_cmemcache = True
00283             except ImportError:
00284                 try:
00285                     import memcache
00286                     is_cmemcache = False
00287                 except ImportError:
00288                     raise RuntimeError('no memcache module found')
00289 
00290             # cmemcache has a bug that debuglog is not defined for the
00291             # client.  Whenever pickle fails you get a weird AttributError.
00292             if is_cmemcache:
00293                 client = memcache.Client(map(str, servers))
00294                 try:
00295                     client.debuglog = lambda *a: None
00296                 except:
00297                     pass
00298             else:
00299                 client = memcache.Client(servers, False, HIGHEST_PROTOCOL)
00300         else:
00301             client = servers
00302 
00303         self._client = client
00304         self.key_prefix = key_prefix


Member Function Documentation

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.add (   self,
  key,
  value,
  timeout = None 
)
Works like :meth:`set` but does not override already existing
values.

:param key: the key to set
:param value: the value for the key
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 343 of file cache.py.

00343 
00344     def add(self, key, value, timeout=None):
00345         if timeout is None:
00346             timeout = self.default_timeout
00347         if isinstance(key, unicode):
00348             key = key.encode('utf-8')
00349         if self.key_prefix:
00350             key = self.key_prefix + key
00351         self._client.add(key, value, timeout)

Here is the caller graph for this function:

Clears the cache.  Keep in mind that not all caches support
clearning of the full cache.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 396 of file cache.py.

00396 
00397     def clear(self):
00398         self._client.flush_all()

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.dec (   self,
  key,
  delta = 1 
)
Decrements the value of a key by `delta`.  If the key does
not yet exist it is initialized with `-delta`.

For supporting caches this is an atomic operation.

:param key: the key to increment.
:param delta: the delta to subtract.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 406 of file cache.py.

00406 
00407     def dec(self, key, delta=1):
00408         if isinstance(key, unicode):
00409             key = key.encode('utf-8')
00410         if self.key_prefix:
00411             key = self.key_prefix + key
00412         self._client.decr(key, delta)
00413 

Deletes `key` from the cache.  If it does not exist in the cache
nothing happens.

:param key: the key to delete.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 377 of file cache.py.

00377 
00378     def delete(self, key):
00379         if isinstance(key, unicode):
00380             key = key.encode('utf-8')
00381         if self.key_prefix:
00382             key = self.key_prefix + key
00383         if _test_memcached_key(key):
00384             self._client.delete(key)

Deletes multiple keys at once.

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 385 of file cache.py.

00385 
00386     def delete_many(self, *keys):
00387         new_keys = []
00388         for key in keys:
00389             if isinstance(key, unicode):
00390                 key = key.encode('utf-8')
00391             if self.key_prefix:
00392                 key = self.key_prefix + key
00393             if _test_memcached_key(key):
00394                 new_keys.append(key)
00395         self._client.delete_multi(new_keys)

Looks up key in the cache and returns it.  If the key does not
exist `None` is returned instead.

:param key: the key to be looked up.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 305 of file cache.py.

00305 
00306     def get(self, key):
00307         if isinstance(key, unicode):
00308             key = key.encode('utf-8')
00309         if self.key_prefix:
00310             key = self.key_prefix + key
00311         # memcached doesn't support keys longer than that.  Because often
00312         # checks for so long keys can occour because it's tested from user
00313         # submitted data etc we fail silently for getting.
00314         if _test_memcached_key(key):
00315             return self._client.get(key)

Here is the caller graph for this function:

Works like :meth:`get_many` but returns a dict::

    d = cache.get_dict("foo", "bar")
    foo = d["foo"]
    bar = d["bar"]

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 316 of file cache.py.

00316 
00317     def get_dict(self, *keys):
00318         key_mapping = {}
00319         have_encoded_keys = False
00320         for idx, key in enumerate(keys):
00321             if isinstance(key, unicode):
00322                 encoded_key = key.encode('utf-8')
00323                 have_encoded_keys = True
00324             else:
00325                 encoded_key = key
00326             if self.key_prefix:
00327                 encoded_key = self.key_prefix + encoded_key
00328             if _test_memcached_key(key):
00329                 key_mapping[encoded_key] = key
00330         # the keys call here is important because otherwise cmemcache
00331         # does ugly things.  What exaclty I don't know, i think it does
00332         # Py_DECREF but quite frankly i don't care.
00333         d = rv = self._client.get_multi(key_mapping.keys())
00334         if have_encoded_keys or self.key_prefix:
00335             rv = {}
00336             for key, value in d.iteritems():
00337                 rv[key_mapping[key]] = value
00338         if len(rv) < len(keys):
00339             for key in keys:
00340                 if key not in rv:
00341                     rv[key] = None
00342         return rv

Returns a list of keys.  For each key a item in the list is
created.  Example::

    foo, bar = cache.get_many("foo", "bar")

If a key can't be looked up `None` is returned for that key
instead.

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 361 of file cache.py.

00361 
00362     def get_many(self, *keys):
00363         d = self.get_dict(*keys)
00364         return [d[key] for key in keys]

Here is the call graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.inc (   self,
  key,
  delta = 1 
)
Increments the value of a key by `delta`.  If the key does
not yet exist it is initialized with `delta`.

For supporting caches this is an atomic operation.

:param key: the key to increment.
:param delta: the delta to add.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 399 of file cache.py.

00399 
00400     def inc(self, key, delta=1):
00401         if isinstance(key, unicode):
00402             key = key.encode('utf-8')
00403         if self.key_prefix:
00404             key = self.key_prefix + key
00405         self._client.incr(key, delta)

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.set (   self,
  key,
  value,
  timeout = None 
)
Adds or overrides a key in the cache.

:param key: the key to set
:param value: the value for the key
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 352 of file cache.py.

00352 
00353     def set(self, key, value, timeout=None):
00354         if timeout is None:
00355             timeout = self.default_timeout
00356         if isinstance(key, unicode):
00357             key = key.encode('utf-8')
00358         if self.key_prefix:
00359             key = self.key_prefix + key
00360         self._client.set(key, value, timeout)

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.set_many (   self,
  mapping,
  timeout = None 
)
Sets multiple keys and values from a dict.

:param mapping: a dict with the values to set.
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 365 of file cache.py.

00365 
00366     def set_many(self, mapping, timeout=None):
00367         if timeout is None:
00368             timeout = self.default_timeout
00369         new_mapping = {}
00370         for key, value in mapping.iteritems():
00371             if isinstance(key, unicode):
00372                 key = key.encode('utf-8')
00373             if self.key_prefix:
00374                 key = self.key_prefix + key
00375             new_mapping[key] = value
00376         self._client.set_multi(new_mapping, timeout)


Member Data Documentation

Definition at line 302 of file cache.py.

Definition at line 79 of file cache.py.

Definition at line 303 of file cache.py.


The documentation for this class was generated from the following file: