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:

    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)

    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)

    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

    Example:

    1. @cache.memoize()
    2. def add(self, b):
    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. 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 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:

    get(*args, **kwargs)

    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 .

    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.
    • 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.

    Proxy function for internal cache object.