Cement defines a Cache Interface, but does not implement caching by default.
Cement often includes multiple handler implementations of an interface that may or may not have additional features or functionality than the interface requires. The documentation below only references usage based on the interface and default handler (not the full capabilities of an implementation).
Cement Extensions That Provide Cache Handlers:
API References:
​Cement Core Cache Module​
The following options under App.Meta
modify configuration handling:
Option | Description |
cache_handler | The handler that implements the cache interface. |
The following example uses the Memcached Extension, which requires the pylibmc
library to be installed, as well as a Memcached server running on localhost:11211
.
from cement import Appfrom cement.utils.misc import init_defaults​CONFIG = init_defaults('myapp', 'memcached')CONFIG['cache.memcached']['expire_time'] = 300 # secondsCONFIG['cache.memcached']['hosts'] = ['127.0.0.1']​class MyApp(App):class Meta:label = 'myapp'config_defaults = CONFIGextensions = ['memcached']cache_handler = 'memcached'​with MyApp() as app:# Run the appapp.run()​# Set a cached valueapp.cache.set('my_key', 'my value')​# Get a cached valueapp.cache.get('my_key')​# Delete a cached valueapp.cache.delete('my_key')​# Delete the entire cacheapp.cache.purge()
All interfaces in Cement can be overridden with your own implementation. This can be done either by sub-classing CacheHandler
itself, or by sub-classing an existing extension's handlers in order to alter their functionality.
myapp.pyfrom cement import Appfrom cement.core.cache import CacheHandler​class MyCacheHandler(CacheHandler):class Meta:label = 'my_cache_handler'​# do something to implement the interface​class MyApp(App):class Meta:label = 'myapp'cache_handler = 'my_cache_handler'handlers = [MyCacheHandler,]