simple caching for deterministic functions with cache key generation based on argument values and bytecode hashing. api compliant with both flask and django. (still experimental/work-in-progress, but api stable)
Switch branches/tags
Nothing to show
Pull request Compare This branch is 26 commits behind tkaemming:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Provides facilities for memozing function calls using pluggable cache backends. Cache keys are (by default) automatically generated based on the function's bytecode (along with the *args/**kwargs that the function receives).


  • Easy to use decorator implementation
  • Implicit versioning of cache keys based on generated bytecode
  • Generation of cache keys from hashed argument values
  • API compliant with Django and Flask, and simple to create your own cache backends if your cache backend of choice is not already supported.


  • Bytecode is not guaranteed to be the same between different interpreter versions or implementations (for example, Python 2.6 to 2.7, or CPython to PyPy.) In many cases the bytecode will be identical, but not always. Upgrading Python versions could result in a variation of the thundering herd problem.
  • All arguments values must implement the __hash__ method, and this method must be deterministic between interpreter restarts, etc. This makes it impossible to use lists or hashes as arguments to a cached function, unless you use a custom key generation function, or convert the values into a hashable type before calling the cache-wrapped function.

autocache.cached(backend, **kwargs)

  • key: use a user-defined cache key (not versioned) instead of hashing the function's bytecode
  • key_generator: use a user-defined cache key generator instead of using __hash__ on the args/kwargs passed to the callable
  • set_kwargs: keyword arguments passed to the cache backend's set method, so you can pass timeouts, etc. when setting cached values

Example use

For example, caching a function with Django's cache backend:

from autocache import cached
from django.core.cache import cache

def expensive_function(x):
    return x ** x

# This will be invoked, and the result will be stored in cache.

# This will be served directly from cache.

# This will be invoked, and the result will be stored in the cache
# as a new value (since `x=100` in this case)

Using a user-defined base key

TODO - No implicit bytecode versioning

Using a user-defined key function

TODO - Make sure arguments are hashable, refactor out hashed tuple generation, etc

Passing additional keyword arguments when setting cache values

TODO - Providing cache timeout values, etc

Writing your own cache backend


Contributing — Quickstart

# optionally add "-p /usr/local/bin/pypy " argument if you have
# pypy installed and want to use it
virtualenv --no-site-packages autocache
cd autocache
echo "export PIP_RESPECT_VIRTUALENV=true" >> bin/activate
source bin/activate
git clone git:// repo
cd repo
make test


MIT Licensed, see LICENSE for the full text.