Caching for humans
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/cache Hash arguments list more robustly. Aug 13, 2012
test remove the assertion that lists can't be passed Aug 21, 2012
.gitignore initial import Apr 5, 2012
LICENSE huge ugly commit. Apr 5, 2012
Makefile don't allow an upload unless tests pass May 3, 2012
README.md move the default setting into the decorator Apr 17, 2012
requirements.txt
setup.py change to Development Status :: 5 May 3, 2012

README.md

cache: caching for humans

Installation

pip install cache

Usage:

import pylibmc
from cache import Cache

backend = pylibmc.Client(["127.0.0.1"])

cache = Cache(backend)

@cache("mykey")
def some_expensive_method():
    sleep(10)
    return 42

# writes 42 to the cache
some_expensive_method()

# reads 42 from the cache
some_expensive_method()

# re-calculates and writes 42 to the cache
some_expensive_method.refresh()

# get the cached value or throw an error
# (unless default= was passed to @cache(...))
some_expensive_method.cached()

Options

Options can be passed to either the Cache constructor or the decorator. Options passed to the decorator take precedence. Available options are:

enabled    If `False`, the backend cache will not be used at all,
           and your functions will be run as-is, even when you call
           `.cached()`.  This is useful for development, when the
           function may be changing rapidly.
           Default: True

bust       If `True`, the values in the backend cache will be
           ignored, and new data will be calculated and written
           over the old values.
           Default: False

default    If given, `.cached()` will return the given value instead
           of raising a KeyError.

The remaining options, if given, will be passed as keyword arguments to the backend's set method. This is useful for things like expiration times - for example, using pylibmc:

@cache("some_key", time=1000)
def expensive_method():
    # ...

Local Caches

Cache provides two "fake" caches for local development without a backend cache: LocalCache and NullCache. LocalCache uses a dictionary in place of a backend cache, and NullCache is a noop on set and always returns None on get.

The difference between passing enabled=False to the cache and using NullCache comes in when you use the .cached() method. If the cache is disabled, .cached() will run the underlying function, but NullCache will throw a KeyError as if the key was not present.

P.S.

If you're a Ruby user, check out the analogous Cacher library for Ruby