cement.ext.ext_memcached

Cement memcached extension module.

Note This extension has an external dependency on pylibmc. Cement explicitly does not include external dependencies for optional extensions.

  • In Cement >=3.0.8 you must include cement[memcached] in your applications dependencies.

  • In Cement <3.0.8 you must include pylibmc in your applications dependencies.

class cement.ext.ext_memcached.MemcachedCacheHandler(*args: Any, **kw: Any)[source]

Bases: CacheHandler

This class implements the Cache Handler interface. It provides a caching interface using the pylibmc library.

class Meta[source]

Bases: Meta

Handler meta-data.

config_defaults: Dict[str, Any] | None = {'expire_time': 0, 'hosts': ['127.0.0.1']}

A config dictionary that is merged into the applications config in the [<config_section>] block. These are defaults and do not override any existing defaults under that section.

label: str = 'memcached'

The string identifier of this handler.

_config(key: str) Any[source]

This is a simple wrapper, and is equivalent to: self.app.config.get('cache.memcached', <key>).

Parameters:

key (str) – The key to get a config value from the ‘cache.memcached’ config section.

Returns:

The value of the given key.

Return type:

unknown

_fix_hosts() None[source]

Useful to fix up the hosts configuration (i.e. convert a comma-separated string into a list). This function does not return anything, however it is expected to set the hosts value of the [cache.memcached] section (which is what this extension reads for it’s host configution).

Returns:

None

_setup(*args: Any, **kw: Any) None[source]

Called during application initialization and must setup the handler object making it ready for the framework or the application to make further calls to it.

Parameters:

app (instance) – The application object.

delete(key: str, **kw: Any) bool[source]

Delete an item from the cache for the given key. Any additional keyword arguments will be passed directly to the pylibmc delete function.

Parameters:

key (str) – The key to delete from the cache.

get(key: str, fallback: Any = None, **kw: Any) Any[source]

Get a value from the cache. Any additional keyword arguments will be passed directly to pylibmc get function.

Parameters:

key (str) – The key of the item in the cache to get.

Keyword Arguments:

fallback – The value to return if the item is not found in the cache.

Returns:

The value of the item in the cache, or the fallback value.

Return type:

unknown

purge(**kw: Any) None[source]

Purge the entire cache, all keys and values will be lost. Any additional keyword arguments will be passed directly to the pylibmc flush_all() function.

set(key: str, value: Any, time: int | None = None, **kw: Any) None[source]

Set a value in the cache for the given key. Any additional keyword arguments will be passed directly to the pylibmc set function.

Parameters:
  • key (str) – The key of the item in the cache to set.

  • value – The value of the item to set.

Keyword Arguments:

time (int) – The expiration time (in seconds) to keep the item cached. Defaults to expire_time as defined in the applications configuration.