Memcache

Ferris provides a couple of decorators to help with using the App Engine Memcache API. Caching items in memcache can significantly reduce the cost of running your application while at the same time making your application more responsive.

ferris.core.memcache.cached(key, ttl=0)[source]

Decorator that given a cache key and optionally a time to live will automatically cache the result of a function in memcache. The next time the function is called it will return the result from memcache (if it’s still there). This decorator does not take arguments to the wrapped function into account- you can use cached_by_args for that.

This function also adds the cached, uncached, and clear_cache functions to the wrapped function that allow you to get the cached and uncached values and clear the cache.

For example:

from ferris import cached

@cached('my-key')
def do_something_slow():
    return herd_cats()

# First call will prime memcache, subsequent calls will get the result from memcache.
r = do_something_slow()

# This skips memcache and always returns a fresh value. Does not update memcache.
r = do_something_slow.uncached()

# This clears the value in memcache so that the subsequent call returns a fresh value.
do_something_slow.clear_cache()
ferris.core.memcache.cached_by_args(key, ttl=0)[source]

Similar to @cached, but takes arguments into account. It will turn each argument into a string an use it as part of the key. If the first argument is ‘self’ or ‘cls’, it will ignore it.