baseplate.context.memcache

Configuration Parsing

baseplate.context.memcache.pool_from_config(app_config, prefix=u'memcache.', serializer=None, deserializer=None)

Make a PooledClient from a configuration dictionary.

The keys useful to pool_from_config() should be prefixed, e.g. memcache.endpoint, memcache.max_pool_size, etc. The prefix argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the PooledClient constructor.

Supported keys:

  • endpoint (required): a string representing a host and port to connect
    to memcached service, e.g. localhost:11211 or 127.0.0.1:11211.
  • max_pool_size: an integer for the maximum pool size to use, by default
    this is 2147483648.
  • connect_timeout: a float representing seconds to wait for a connection to
    memcached server. Defaults to the underlying socket default timeout.
  • timeout: a float representing seconds to wait for calls on the
    socket connected to memcache. Defaults to the underlying socket default timeout.
Parameters:
  • app_config (dict) – the config dictionary
  • prefix (str) – prefix for config keys
  • serializer (callable) – function to serialize values to strings suitable for being stored in memcached. An example is make_dump_and_compress_fn().
  • deserializer (callable) – function to convert strings returned from memcached to arbitrary objects, must be compatible with serializer. An example is decompress_and_load().
Returns:

pymemcache.client.base.PooledClient

Classes

class baseplate.context.memcache.MemcacheContextFactory(pooled_client)

Memcache client context factory.

This factory will attach a MonitoredMemcacheConnection to an attribute on the context object. When memcache commands are executed via this connection object, they will use connections from the provided PooledClient and automatically record diagnostic information.

Parameters:pooled_client (pymemcache.client.base.PooledClient) – A pooled client.
Returns:MonitoredMemcacheConnection
class baseplate.context.memcache.MonitoredMemcacheConnection(context_name, server_span, pooled_client)

Memcache connection that collects diagnostic information.

This connection acts like a PooledClient except that operations are wrapped with diagnostic collection. Some methods may not yet be wrapped with monitoring. Please request assistance if any needed methods are not being monitored.

Serialization/deserialization helpers

baseplate.context.memcache.lib.decompress_and_load(key, serialized, flags)

Deserialize data.

This should be paired with make_dump_and_compress_fn().

Parameters:
  • key (str) – the memcached key.
  • serialized (str) – the serialized object returned from memcached.
  • flags (int) – value stored and returned from memcached for the client to use to indicate how the value was serialized.
Returns:

The deserialized value.

baseplate.context.memcache.lib.make_dump_and_compress_fn(min_compress_length=0, compress_level=1)

Make a serializer.

This should be paired with decompress_and_load().

The resulting method is a chain of json.loads() and zlib compression. Values that are not JSON serializable will result in a TypeError.

Parameters:
  • min_compress_length (int) – the minimum serialized string length to enable zlib compression. 0 disables compression.
  • compress_level (int) – zlib compression level. 0 disables compression and 9 is the maximum value.
Returns:

The serializer.

baseplate.context.memcache.lib.decompress_and_unpickle(key, serialized, flags)

Deserialize data stored by pylibmc.

Warning

This should only be used when sharing caches with applications using pylibmc (like r2). New applications should use the safer and future proofed decompress_and_load().

Parameters:
  • key (str) – the memcached key.
  • serialized (str) – the serialized object returned from memcached.
  • flags (int) – value stored and returned from memcached for the client to use to indicate how the value was serialized.
Returns str value:
 

the deserialized value.

baseplate.context.memcache.lib.make_pickle_and_compress_fn(min_compress_length=0, compress_level=1)

Make a serializer compatible with pylibmc readers.

The resulting method is a chain of pickle.dumps() and zlib compression. This should be paired with decompress_and_unpickle().

Warning

This should only be used when sharing caches with applications using pylibmc (like r2). New applications should use the safer and future proofed make_dump_and_compress_fn().

Parameters:
  • min_compress_length (int) – the minimum serialized string length to enable zlib compression. 0 disables compression.
  • compress_level (int) – zlib compression level. 0 disables compression and 9 is the maximum value.
Returns func memcache_serializer:
 

the serializer method.