Memcached

Introduction

The Memcached Extension includes the MemcachedCacheHandler and provides application caching and key/value store support via Memcached.

Documentation References:

API References:

Requirements

Cement 3.0.8+:

pip install cement[memcached]

Applications using Cement <3.0.8 should continue to include pylibmc in their dependencies.

Configuration

This extension honors the following config settings under a [cache.memcached] section in any configuration file:

Usage

myapp.py
from cement import App
from cement.utils.misc import init_defaults

CONFIG = init_defaults('myapp', 'cache.memcached')
CONFIG['cache.memcached']['expire_time'] = 300 # seconds
CONFIG['cache.memcached']['hosts'] = ['127.0.0.1']

class MyApp(App):
    class Meta:
        label = 'myapp'
        config_defaults = CONFIG
        extensions = ['memcached']
        cache_handler = 'memcached'

with MyApp() as app:
    # Run the app
    app.run()

    # Set a cached value
    app.cache.set('my_key', 'my value')

    # Get a cached value
    app.cache.get('my_key')

    # Delete a cached value
    app.cache.delete('my_key')

    # Delete the entire cache
    app.cache.purge()
~/.myapp.conf
[myapp]

# set the cache handler to use
cache_handler = memcached


[cache.memcached]

# time in seconds that an item in the cache will expire
expire_time = 3600

# comma seperated list of memcached servers
hosts = 127.0.0.1, cache.example.com

Last updated