Exception as Misses Mapping

Introduction

This is a subclass of Memcache which swallows exceptions and treats them like misses. This is meant to allow code to be a bit simpler, rather than catching all exceptions, you can just do things like the below example.

Examples

Basic example:

>>> import memcached2
>>> mc = memcached2.ExceptionsAreMissesMemcache(('memcached://localhost/',))
>>> data = mc.get('key')
>>> if not data:
>>>    data = [compute data]
>>>    mc.set('key', data)
>>> [use data]

Object Documentation

class memcached2.ExceptionsAreMissesMemcache(servers, value_wrapper=None, selector=None, hasher=None, server_pool=None)

A Memcache wrapper class which swallows server exceptions, except in the case of coding errors. This is meant for situations where you want to keep the code simple, and treat cache misses, server errors, and the like as cache misses. See memcached2.Memcache() for details of the use of this class, exceptions to that are noted here.

The methods that are protected against exceptions are those documented in this class. Everything should otherwise act like a Memcache instance.

Parameters:
  • servers (list) – One or more server URIs of the form: “memcache://hostname[:port]/”
  • value_wrapper (ValueSuperStr or compatible object.) – (None) This causes values returned to be wrapped in the passed class before being returned. For example ValueSuperStr implements many useful additions to the string return.
  • selector (SelectorBase) – (None) This code implements the server selector logic. If not specified, the default is used. The default is to use SelectorFirst if only one server is specified, and SelectorRehashDownServers if multiple servers are given.
  • hasher (HasherBase) – (None) A “Hash” object which takes a key and returns a hash for persistent server selection. If not specified, it defaults to HasherZero if there is only one server specified, or HasherCMemcache otherwise.
  • server_pool (ServerPool object.) – (None) A server connection pool. If not specified, a global pool is used.
delete(*args, **kwargs)

Remove this key from the server.

Exceptions are swallowed and treated as memcached misses. See delete() for details on this method. Changes from the base function are:

Raises:Exceptions are swallowed and treated a misses.
get(*args, **kwargs)

Retrieve the specified key from a memcache server.

Exceptions are swallowed and treated as memcached misses. See get() for details on this method. Changes from the base function are:

Returns:None if no value or exception, String, or “value_wrapper” as specified during object creation such as ~memcached2.ValueSuperStr.
Raises:Exceptions are swallowed and treated a misses.
set(*args, **kwargs)

Update the value in the server. See set() for details on this method. Changes from the base function are:

Exceptions are swallowed and treated as memcached misses. See set() for details on this method. Changes from the base function are:

Raises:Exceptions are swallowed and treated a misses.
set_multi(*args, **kwargs)

Update multiple values in the server. See set_multi() for details on this method. Changes from the base function are:

Exceptions are swallowed and treated as memcached misses. See set() for details on this method. Changes from the base function are:

Raises:Exceptions are swallowed and treated a misses.