memcached_memoize

memcached_memoize is decorator to make easy to use Django's cache backends to memoize python functions.


Keywords
memcached_memoize
License
MIT
Install
pip install memcached_memoize==0.2.1

Documentation

what?

A decorator for memoizing python functions using Django cache backend.

why?

Because cache should be easy to implement and shouldn't be part of your business logic.

usage

from memcached_memoize.decorators import memcached

@memcached('30s')
def cached_method():
    return 'value to be cached for 30 seconds.'

The returning value of the function above will be cached for 30 seconds using django's cache backends.

circuit breaker

An implementation of the circuit breaker pattern is also available using the memcached as fallback if a data source is unavailable. You can use it like this:

from memcached_memoize.decorators.circuit_breaker import memcached_circuit_breaker

@memcached_circuit_breaker(max_fail=5, stale_expires='30s')
def cached_method():
    return data_source.get()

If the data_source.get() is available, the code above will return the value from it and save it in the cache. Whenever the data_source.get() raises error, the code will return the value from cache. If it raises as many errors as the value of max_fail, the value from cache will be returned until the stale_expires time.