LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction to the Cache Interface
  • Configuration
  • Application Meta Options
  • Working with Caches
  • Creating a Cache Handler
  1. Core Foundation

Caching

PreviousOutput RenderingNextMail Messaging

Last updated 6 years ago

Introduction to the Cache Interface

Cement defines a , 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:

Configuration

Application Meta Options

The following options under modify configuration handling:

Option

Description

cache_handler

The handler that implements the cache interface.

Working with Caches

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

CONFIG = init_defaults('myapp', '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()

Creating a Cache Handler

myapp.py
from cement import App
from 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,
        ]

The following example uses the , which requires the pylibmc library to be installed, as well as a Memcached server running on localhost:11211.

All interfaces in Cement can be overridden with your own implementation. This can be done either by sub-classing itself, or by sub-classing an existing extension's handlers in order to alter their functionality.

Cache Interface
Memcached
Redis
Cement Core Cache Module
App.Meta
Memcached Extension
CacheHandler