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.
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:
@cache.memoize(50)
def random_func():
return random.randrange(1, 50)
@cache.memoize()
def param_func(a, b):
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:
@cache.memoize()
def add(self, b):
>>> adder1 = Adder()
>>> adder2 = Adder()
>>> adder1.add(3)
3.23214234
>>> adder2.add(3)
3.60898509
>>> cache.delete_memoized(adder.add)
>>> adder1.add(3)
3.01348673
>>> adder2.add(3)
3.60898509
>>> cache.delete_memoized(Adder.add)
>>> adder1.add(3)
3.53235667
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:
@cache.memoize(timeout=50)
def big_foo(a, b):
return a + b + random.randrange(0, 1000)
>>> big_foo(5, 2)
753
>>> big_foo(5, 3)
234
>>> big_foo(5, 2)
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.