Rosendale Simple Caching Plugin

The SimpleCache Plugin provides quick management of a cPickle key/value data store or cache. The following features are included:

  • clear-cache removes the cache file
  • show-cache displays the key/value pairs in the cache
  • –no-cache option prevents any data from being cached

The simplecache plugin is Open Source and distributed under The MIT License. Please see the LICENSE file included with The Rosendale Project.

Installation

Stable versions of the rosendale.simplecache plugin are available via PyPi and can be installed by easy_install, pip, or similar:

$ easy_install rosendale.simplecache

You will want to add ‘rosendale.simplecache’ to your applications ‘setup.py’ under the section ‘install_requires’.

Configuration

The simplecache plugin can be enabled by adding a plugin config under your applications ‘plugin_config_dir’ (set via your applications primary config file). I.e:

/etc/helloworld/plugins.d/simplecache.conf

[simplecache]
enable_plugin = true
provider = rosendale
cache_file = ~/.helloworld.cache
default_expiration = 3600
create_mode = 0600

This plugin does not honor any other config settings than the above. The above ‘cache_file’ sets a ‘per user cache’, but is not necessary and depends on how simple cache being used within your application. Simply note that if the cache file is not dynamic per user, all users will access the same cache file.

Usage Within Your Application

To demonstrate usage we have created a fresh ‘helloworld’ application via the paster cement-app helper. We’ve then enabled the rosendale plugin by creating the file ‘~/.helloworld/etc/plugins.d/simplecache.conf’ with the config listed above.

$ helloworld --help
loading simplecache plugin
Usage:   helloworld [COMMAND] --(OPTIONS)

Commands:
    get-started, clear-cache, cmd1

Help?  try '[COMMAND]-help' OR '[NAMESPACE] --help'

Options:
    --version          show program's version number and exit
    -h, --help         show this help message and exit
    -R, --root-option  Example root option
    --json             render output as json (CLI-API)
    --debug            toggle debug output
    --quiet            disable console logging
    --no-cache         do not cache any data

You can see the additional command ‘clear-cache’ as well as the option ‘–no-cache’. Additionally there is a hidden ‘show-cache’ command. Further usage is explained below.

Accessing The Cache Within Your Application

The cache object can be created anywhere, however to demonstrate we will expose a command to our helloworld applications ‘root’ controller called ‘cache-test’.

from rosendale.lib.simplecache import SimpleCache

class RootController(CementController):
    @expose()
    def cache_test(self):
        "Store some key/value to the cache, then retrieve it."
        cache = SimpleCache(renew_on_get=True)
        cache.store('my_key', 'Some Value')
        print cache.get('my_key')
        return dict()

And the output is:

$ helloworld cache-test
loading simplecache plugin
Some Value

The concept and implementation is simple. Now that we have something in our cache, lets look at it:

$ helloworld show-cache
loading simplecache plugin
my_key => Some Value (expires: 2010-08-23 02:15:16.902930)

Notice that because we passed ‘renew_on_get’ the expire time increases on access:

$ helloworld cache-test
loading simplecache plugin
Some Value

$ helloworld show-cache
loading simplecache plugin
my_key => Some Value (expires: 2010-08-23 02:17:18.429827)

And we can clear the cache:

$ helloworld clear-cache

Which removes all keys from the cache file. And finally, using the ‘–no-cache’ option means no cache is stored:

$ helloworld cache-test --no-cache