API

class flask.ext.cache.Cache(app=None, with_jinja2_ext=True, config=None)

This class is used to control the cache objects.

add(*args, **kwargs)

Proxy function for internal cache object.

cached(timeout=None, key_prefix='view/%s', unless=None)

Decorator. Use this to cache a function. By default the cache key is view/request.path. You are able to use this decorator with any function by changing the key_prefix. If the token %s is located within the key_prefix then it will replace that with request.path

Example:

  1. # An example view function
  2. @cache.cached(timeout=50)
  3. def big_foo():
  4. return big_bar_calc()
  5. # An example misc function to cache.
  6. @cache.cached(key_prefix='MyCachedList')
  7. def get_list():
  8. return [random.randrange(0, 1) for i in range(50000)]
  9. my_list = get_list()

Note

You MUST have a request context to actually called any functions that are cached.

New in version 0.4: The returned decorated function now has three function attributes assigned to it. These attributes are readable/writable.

uncached

The original undecorated function

cache_timeout

The cache timeout value for this function. For a custom value to take affect, this must be set before the function is called.

make_cache_key

A function used in generating the cache_key used.

Parameters:

  • timeout – Default None. If set to an integer, will cache for that amount of time. Unit of time is in seconds.
  • key_prefix

    Default ‘view/%(request.path)s’. Beginning key to . use for the cache key.

    1. New in version 0.3.4: Can optionally be a callable which takes no arguments but returns a string that will be used as the cache_key.
    • unless – Default None. Cache will always execute the caching facilities unless this callable is true. This will bypass the caching entirely.

clear()

Proxy function for internal cache object.

delete(*args, **kwargs)

Proxy function for internal cache object.

delete_many(*args, **kwargs)

Proxy function for internal cache object.

delete_memoized(f, *args, **kwargs)

Deletes the specified functions caches, based by given parameters. If parameters are given, only the functions that were memoized with them will be erased. Otherwise all versions of the caches will be forgotten.

Example:

  1. @cache.memoize(50)
  2. def random_func():
  3. return random.randrange(1, 50)
  4. @cache.memoize()
  5. def param_func(a, b):
  6. return a+b+random.randrange(1, 50)
  1. >>> random_func()
  2. 43
  3. >>> random_func()
  4. 43
  5. >>> cache.delete_memoized('random_func')
  6. >>> random_func()
  7. 16
  8. >>> param_func(1, 2)
  9. 32
  10. >>> param_func(1, 2)
  11. 32
  12. >>> param_func(2, 2)
  13. 47
  14. >>> cache.delete_memoized('param_func', 1, 2)
  15. >>> param_func(1, 2)
  16. 13
  17. >>> param_func(2, 2)
  18. 47

Delete memoized is also smart about instance methods vs class methods.

When passing a instancemethod, it will only clear the cache related to that instance of that object. (object uniqueness can be overridden

When passing a classmethod, it will clear all caches related across all instances of that class.

Example:

  1. class Adder(object):
  2. @cache.memoize()
  3. def add(self, b):
  4. return b + random.random()
  1. >>> adder1 = Adder()
  2. >>> adder2 = Adder()
  3. >>> adder1.add(3)
  4. 3.23214234
  5. >>> adder2.add(3)
  6. 3.60898509
  7. >>> cache.delete_memoized(adder.add)
  8. >>> adder1.add(3)
  9. 3.01348673
  10. >>> adder2.add(3)
  11. 3.60898509
  12. >>> cache.delete_memoized(Adder.add)
  13. >>> adder1.add(3)
  14. 3.53235667
  15. >>> adder2.add(3)
  16. 3.72341788

Parameters:

  • fname – Name of the memoized function, or a reference to the function.
  • *args – A list of positional parameters used with memoized function.
  • **kwargs – A dict of named parameters used with memoized function.

Note

Flask-Cache uses inspect to order kwargs into positional args when the function is memoized. If you pass a function reference into fname instead of the function name, Flask-Cache will be able to place the args/kwargs in the proper order, and delete the positional cache.

However, if delete_memoized is just called with the name of the function, be sure to pass in potential arguments in the same order as defined in your function as args only, otherwise Flask-Cache will not be able to compute the same cache key.

Note

Flask-Cache maintains an internal random version hash for the function. Using delete_memoized will only swap out the version hash, causing the memoize function to recompute results and put them into another key.

This leaves any computed caches for this memoized function within the caching backend.

It is recommended to use a very high timeout with memoize if using this function, so that when the version has is swapped, the old cached results would eventually be reclaimed by the caching backend.

delete_memoized_verhash(f, *args)

Delete the version hash associated with the function.

..warning:

  1. Performing this operation could leave keys behind that have
  2. been created with this version hash. It is up to the application
  3. to make sure that all keys that may have been created with this
  4. version hash at least have timeouts so they will not sit orphaned
  5. in the cache backend.

get(*args, **kwargs)

Proxy function for internal cache object.

get_many(*args, **kwargs)

Proxy function for internal cache object.

init_app(app, config=None)

This is used to initialize cache with your app object

memoize(timeout=None, make_name=None, unless=None)

Use this to cache the result of a function, taking its arguments into account in the cache key.

Information on Memoization.

Example:

  1. @cache.memoize(timeout=50)
  2. def big_foo(a, b):
  3. return a + b + random.randrange(0, 1000)
  1. >>> big_foo(5, 2)
  2. 753
  3. >>> big_foo(5, 3)
  4. 234
  5. >>> big_foo(5, 2)
  6. 753

New in version 0.4: The returned decorated function now has three function attributes assigned to it.

uncached

The original undecorated function. readable only

cache_timeout

The cache timeout value for this function. For a custom value to take affect, this must be set before the function is called.

readable and writable

make_cache_key

A function used in generating the cache_key used.

readable and writable

Parameters:

  • timeout – Default None. If set to an integer, will cache for that amount of time. Unit of time is in seconds.
  • make_name – Default None. If set this is a function that accepts a single argument, the function name, and returns a new string to be used as the function name. If not set then the function name is used.
  • unless – Default None. Cache will always execute the caching facilities unelss this callable is true. This will bypass the caching entirely.

New in version 0.5: params make_name, unless

set(*args, **kwargs)

Proxy function for internal cache object.

set_many(*args, **kwargs)

Proxy function for internal cache object.