watson.cache.decorators

watson.cache.decorators.cache(func=None, timeout=0, key=None, cache_type=<class 'watson.cache.storage.Memory'>)[source]

Retrieve a value from the cache

Attempts to retrieve a value from the cache. If the wrapped function does not have an attribute of container (see watson.di.container), from which to retrieve the cache type then it will default to cache.storage.Memory.

Parameters:
  • func (callable) – the function that is being wrapped
  • timeout (int) – the number of seconds the item should exist in the cache
  • key (string) – the key to store the data against in the cache, defaults to the qualified name of the decorated function.
Returns:

The contents of the cache key.

Example:

class MyClass(ContainerAware):
    @cache(timeout=3600)
    def expensive_func(self):
        return 'something'

c = MyClass()
c.expensive_func() # something
c.expensive_func() # something - returned from cache