__init__.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. """
  2. flask_caching
  3. ~~~~~~~~~~~~~
  4. Adds cache support to your application.
  5. :copyright: (c) 2010 by Thadeus Burgess.
  6. :license: BSD, see LICENSE for more details.
  7. """
  8. import base64
  9. import functools
  10. import hashlib
  11. import inspect
  12. import logging
  13. import uuid
  14. import warnings
  15. from collections import OrderedDict
  16. from typing import Any
  17. from typing import Callable
  18. from typing import Dict
  19. from typing import List
  20. from typing import Optional
  21. from typing import Tuple
  22. from typing import Union
  23. from flask import current_app
  24. from flask import Flask
  25. from flask import request
  26. from flask import Response
  27. from flask import url_for
  28. from werkzeug.utils import import_string
  29. from flask_caching.backends.base import BaseCache
  30. from flask_caching.backends.simplecache import SimpleCache
  31. from flask_caching.utils import function_namespace
  32. from flask_caching.utils import get_arg_default
  33. from flask_caching.utils import get_arg_names
  34. from flask_caching.utils import get_id
  35. from flask_caching.utils import make_template_fragment_key # noqa: F401
  36. from flask_caching.utils import wants_args
  37. __version__ = "2.3.1"
  38. logger = logging.getLogger(__name__)
  39. SUPPORTED_HASH_FUNCTIONS = [
  40. hashlib.sha1,
  41. hashlib.sha224,
  42. hashlib.sha256,
  43. hashlib.sha384,
  44. hashlib.sha512,
  45. hashlib.md5,
  46. ]
  47. class CachedResponse(Response):
  48. """
  49. views wraped by @cached can return this (which inherits from flask.Response)
  50. to override the cache TTL dynamically
  51. """
  52. timeout = None
  53. def __init__(self, response, timeout):
  54. self.__dict__ = response.__dict__
  55. self.timeout = timeout
  56. class Cache:
  57. """This class is used to control the cache objects."""
  58. def __init__(
  59. self,
  60. app: Optional[Flask] = None,
  61. with_jinja2_ext: bool = True,
  62. config=None,
  63. ) -> None:
  64. if not (config is None or isinstance(config, dict)):
  65. raise ValueError("`config` must be an instance of dict or None")
  66. self.with_jinja2_ext = with_jinja2_ext
  67. self.config = config
  68. self.source_check = None
  69. if app is not None:
  70. self.init_app(app, config)
  71. def init_app(self, app: Flask, config=None) -> None:
  72. """This is used to initialize cache with your app object"""
  73. if not (config is None or isinstance(config, dict)):
  74. raise ValueError("`config` must be an instance of dict or None")
  75. #: Ref PR #44.
  76. #: Do not set self.app in the case a single instance of the Cache
  77. #: object is being used for multiple app instances.
  78. #: Example use case would be Cache shipped as part of a blueprint
  79. #: or utility library.
  80. base_config = app.config.copy()
  81. if self.config:
  82. base_config.update(self.config)
  83. if config:
  84. base_config.update(config)
  85. config = base_config
  86. config.setdefault("CACHE_DEFAULT_TIMEOUT", 300)
  87. config.setdefault("CACHE_IGNORE_ERRORS", False)
  88. config.setdefault("CACHE_THRESHOLD", 500)
  89. config.setdefault("CACHE_KEY_PREFIX", "flask_cache_")
  90. config.setdefault("CACHE_MEMCACHED_SERVERS", None)
  91. config.setdefault("CACHE_DIR", None)
  92. config.setdefault("CACHE_OPTIONS", None)
  93. config.setdefault("CACHE_ARGS", [])
  94. config.setdefault("CACHE_TYPE", "null")
  95. config.setdefault("CACHE_NO_NULL_WARNING", False)
  96. config.setdefault("CACHE_SOURCE_CHECK", False)
  97. if config["CACHE_TYPE"] == "null" and not config["CACHE_NO_NULL_WARNING"]:
  98. warnings.warn(
  99. "Flask-Caching: CACHE_TYPE is set to null, "
  100. "caching is effectively disabled.",
  101. stacklevel=2,
  102. )
  103. if (
  104. config["CACHE_TYPE"] in ["filesystem", "FileSystemCache"]
  105. and config["CACHE_DIR"] is None
  106. ):
  107. warnings.warn(
  108. f"Flask-Caching: CACHE_TYPE is set to {config['CACHE_TYPE']} but no "
  109. "CACHE_DIR is set.",
  110. stacklevel=2,
  111. )
  112. self.source_check = config["CACHE_SOURCE_CHECK"]
  113. if self.with_jinja2_ext:
  114. from .jinja2ext import CacheExtension, JINJA_CACHE_ATTR_NAME
  115. setattr(app.jinja_env, JINJA_CACHE_ATTR_NAME, self)
  116. app.jinja_env.add_extension(CacheExtension)
  117. self._set_cache(app, config)
  118. def _set_cache(self, app: Flask, config) -> None:
  119. import_me = config["CACHE_TYPE"]
  120. if "." not in import_me:
  121. plain_name_used = True
  122. import_me = "flask_caching.backends." + import_me
  123. else:
  124. plain_name_used = False
  125. cache_factory = import_string(import_me)
  126. cache_args = config["CACHE_ARGS"][:]
  127. cache_options = {"default_timeout": config["CACHE_DEFAULT_TIMEOUT"]}
  128. if isinstance(cache_factory, type) and issubclass(cache_factory, BaseCache):
  129. cache_factory = cache_factory.factory
  130. elif plain_name_used:
  131. warnings.warn(
  132. "Using the initialization functions in flask_caching.backend "
  133. "is deprecated. Use the a full path to backend classes "
  134. "directly.",
  135. category=DeprecationWarning,
  136. stacklevel=2,
  137. )
  138. if config["CACHE_OPTIONS"]:
  139. cache_options.update(config["CACHE_OPTIONS"])
  140. if not hasattr(app, "extensions"):
  141. app.extensions = {}
  142. app.extensions.setdefault("cache", {})
  143. app.extensions["cache"][self] = cache_factory(
  144. app, config, cache_args, cache_options
  145. )
  146. self.app = app
  147. def _call_fn(self, fn, *args, **kwargs):
  148. ensure_sync = getattr(self.app, "ensure_sync", None)
  149. if ensure_sync is not None:
  150. return ensure_sync(fn)(*args, **kwargs)
  151. return fn(*args, **kwargs)
  152. @property
  153. def cache(self) -> SimpleCache:
  154. app = current_app or self.app
  155. return app.extensions["cache"][self]
  156. def get(self, *args, **kwargs) -> Any:
  157. """Proxy function for internal cache object."""
  158. return self.cache.get(*args, **kwargs)
  159. def has(self, *args, **kwargs) -> bool:
  160. """Proxy function for internal cache object."""
  161. return self.cache.has(*args, **kwargs)
  162. def set(self, *args, **kwargs) -> Optional[bool]:
  163. """Proxy function for internal cache object."""
  164. return self.cache.set(*args, **kwargs)
  165. def add(self, *args, **kwargs) -> bool:
  166. """Proxy function for internal cache object."""
  167. return self.cache.add(*args, **kwargs)
  168. def delete(self, *args, **kwargs) -> bool:
  169. """Proxy function for internal cache object."""
  170. return self.cache.delete(*args, **kwargs)
  171. def delete_many(self, *args, **kwargs) -> List[str]:
  172. """Proxy function for internal cache object."""
  173. return self.cache.delete_many(*args, **kwargs)
  174. def clear(self) -> bool:
  175. """Proxy function for internal cache object."""
  176. return self.cache.clear()
  177. def get_many(self, *args, **kwargs):
  178. """Proxy function for internal cache object."""
  179. return self.cache.get_many(*args, **kwargs)
  180. def set_many(self, *args, **kwargs) -> List[Any]:
  181. """Proxy function for internal cache object."""
  182. return self.cache.set_many(*args, **kwargs)
  183. def get_dict(self, *args, **kwargs) -> Dict[str, Any]:
  184. """Proxy function for internal cache object."""
  185. return self.cache.get_dict(*args, **kwargs)
  186. def unlink(self, *args, **kwargs) -> List[str]:
  187. """Proxy function for internal cache object
  188. only support Redis
  189. """
  190. unlink = getattr(self.cache, "unlink", None)
  191. if unlink is not None and callable(unlink):
  192. return unlink(*args, **kwargs)
  193. return self.delete_many(*args, **kwargs)
  194. def cached(
  195. self,
  196. timeout: Optional[int] = None,
  197. key_prefix: str = "view/%s",
  198. unless: Optional[Callable] = None,
  199. forced_update: Optional[Callable] = None,
  200. response_filter: Optional[Callable] = None,
  201. query_string: bool = False,
  202. hash_method: Callable = hashlib.md5,
  203. cache_none: bool = False,
  204. make_cache_key: Optional[Callable] = None,
  205. source_check: Optional[bool] = None,
  206. response_hit_indication: Optional[bool] = False,
  207. ) -> Callable:
  208. """Decorator. Use this to cache a function. By default the cache key
  209. is `view/request.path`. You are able to use this decorator with any
  210. function by changing the `key_prefix`. If the token `%s` is located
  211. within the `key_prefix` then it will replace that with `request.path`
  212. Example::
  213. # An example view function
  214. @cache.cached(timeout=50)
  215. def big_foo():
  216. return big_bar_calc()
  217. # An example misc function to cache.
  218. @cache.cached(key_prefix='MyCachedList')
  219. def get_list():
  220. return [random.randrange(0, 1) for i in range(50000)]
  221. my_list = get_list()
  222. .. note::
  223. You MUST have a request context to actually called any functions
  224. that are cached.
  225. .. versionadded:: 0.4
  226. The returned decorated function now has three function attributes
  227. assigned to it. These attributes are readable/writable.
  228. **uncached**
  229. The original undecorated function
  230. **cache_timeout**
  231. The cache timeout value for this function. For a
  232. custom value to take affect, this must be set before the
  233. function is called.
  234. **make_cache_key**
  235. A function used in generating the cache_key used.
  236. readable and writable
  237. :param timeout: Default None. If set to an integer, will cache for that
  238. amount of time. Unit of time is in seconds.
  239. :param key_prefix: Default 'view/%(request.path)s'. Beginning key to .
  240. use for the cache key. `request.path` will be the
  241. actual request path, or in cases where the
  242. `make_cache_key`-function is called from other
  243. views it will be the expected URL for the view
  244. as generated by Flask's `url_for()`.
  245. .. versionadded:: 0.3.4
  246. Can optionally be a callable which takes
  247. no arguments but returns a string that will
  248. be used as the cache_key.
  249. :param unless: Default None. Cache will *always* execute the caching
  250. facilities unless this callable is true.
  251. This will bypass the caching entirely.
  252. :param forced_update: Default None. If this callable is true,
  253. cache value will be updated regardless cache
  254. is expired or not. Useful for background
  255. renewal of cached functions.
  256. :param response_filter: Default None. If not None, the callable is
  257. invoked after the cached function evaluation,
  258. and is given one argument, the response
  259. content. If the callable returns False, the
  260. content will not be cached. Useful to prevent
  261. caching of code 500 responses.
  262. :param query_string: Default False. When True, the cache key
  263. used will be the result of hashing the
  264. ordered query string parameters. This
  265. avoids creating different caches for
  266. the same query just because the parameters
  267. were passed in a different order. See
  268. _make_cache_key_query_string() for more
  269. details.
  270. :param hash_method: Default hashlib.md5. The hash method used to
  271. generate the keys for cached results.
  272. :param cache_none: Default False. If set to True, add a key exists
  273. check when cache.get returns None. This will likely
  274. lead to wrongly returned None values in concurrent
  275. situations and is not recommended to use.
  276. :param make_cache_key: Default None. If set to a callable object,
  277. it will be called to generate the cache key
  278. :param source_check: Default None. If None will use the value set by
  279. CACHE_SOURCE_CHECK.
  280. If True, include the function's source code in the
  281. hash to avoid using cached values when the source
  282. code has changed and the input values remain the
  283. same. This ensures that the cache_key will be
  284. formed with the function's source code hash in
  285. addition to other parameters that may be included
  286. in the formation of the key.
  287. :param response_hit_indication: Default False.
  288. If True, it will add to response header field 'hit_cache'
  289. if used cache.
  290. """
  291. def decorator(f):
  292. @functools.wraps(f)
  293. def decorated_function(*args, **kwargs):
  294. #: Bypass the cache entirely.
  295. if self._bypass_cache(unless, f, *args, **kwargs):
  296. return self._call_fn(f, *args, **kwargs)
  297. nonlocal source_check
  298. if source_check is None:
  299. source_check = self.source_check
  300. try:
  301. if make_cache_key is not None and callable(make_cache_key):
  302. cache_key = make_cache_key(*args, **kwargs)
  303. else:
  304. cache_key = decorated_function.make_cache_key(
  305. *args, use_request=True, **kwargs
  306. )
  307. if (
  308. callable(forced_update)
  309. and (
  310. forced_update(*args, **kwargs)
  311. if wants_args(forced_update)
  312. else forced_update()
  313. )
  314. is True
  315. ):
  316. rv = None
  317. found = False
  318. else:
  319. rv = self.cache.get(cache_key)
  320. found = True
  321. # If the value returned by cache.get() is None, it
  322. # might be because the key is not found in the cache
  323. # or because the cached value is actually None
  324. if rv is None:
  325. # If we're sure we don't need to cache None values
  326. # (cache_none=False), don't bother checking for
  327. # key existence, as it can lead to false positives
  328. # if a concurrent call already cached the
  329. # key between steps. This would cause us to
  330. # return None when we shouldn't
  331. if not cache_none:
  332. found = False
  333. else:
  334. found = self.cache.has(cache_key)
  335. except Exception:
  336. if self.app.debug:
  337. raise
  338. logger.exception("Exception possibly due to cache backend.")
  339. return self._call_fn(f, *args, **kwargs)
  340. if found and self.app.debug:
  341. logger.info(f"Cache used for key: {cache_key}")
  342. if response_hit_indication:
  343. def apply_caching(response):
  344. if found:
  345. response.headers["hit_cache"] = found
  346. return response
  347. self.app.after_request_funcs[None].append(apply_caching)
  348. if not found:
  349. rv = self._call_fn(f, *args, **kwargs)
  350. if inspect.isgenerator(rv):
  351. rv = [val for val in rv]
  352. if response_filter is None or response_filter(rv):
  353. cache_timeout = decorated_function.cache_timeout
  354. if isinstance(rv, CachedResponse):
  355. cache_timeout = rv.timeout or cache_timeout
  356. try:
  357. self.cache.set(
  358. cache_key,
  359. rv,
  360. timeout=cache_timeout,
  361. )
  362. except Exception:
  363. if self.app.debug:
  364. raise
  365. logger.exception("Exception possibly due to cache backend.")
  366. return rv
  367. def default_make_cache_key(*args, **kwargs):
  368. # Convert non-keyword arguments (which is the way
  369. # `make_cache_key` expects them) to keyword arguments
  370. # (the way `url_for` expects them)
  371. argspec_args = inspect.getfullargspec(f).args
  372. for arg_name, arg in zip(argspec_args, args):
  373. kwargs[arg_name] = arg
  374. use_request = kwargs.pop("use_request", False)
  375. return _make_cache_key(args, kwargs, use_request=use_request)
  376. def _make_cache_key_query_string():
  377. """Create consistent keys for query string arguments.
  378. Produces the same cache key regardless of argument order, e.g.,
  379. both `?limit=10&offset=20` and `?offset=20&limit=10` will
  380. always produce the same exact cache key.
  381. If func is provided and is callable it will be used to hash
  382. the function's source code and include it in the cache key.
  383. This will only be done is source_check is True.
  384. """
  385. # Create a tuple of (key, value) pairs, where the key is the
  386. # argument name and the value is its respective value. Order
  387. # this tuple by key. Doing this ensures the cache key created
  388. # is always the same for query string args whose keys/values
  389. # are the same, regardless of the order in which they are
  390. # provided.
  391. args_as_sorted_tuple = tuple(
  392. sorted(pair for pair in request.args.items(multi=True))
  393. )
  394. # ... now hash the sorted (key, value) tuple so it can be
  395. # used as a key for cache. Turn them into bytes so that the
  396. # hash function will accept them
  397. args_as_bytes = str(args_as_sorted_tuple).encode()
  398. cache_hash = hash_method(args_as_bytes)
  399. # Use the source code if source_check is True and update the
  400. # cache_hash before generating the hashing and using it in
  401. # cache_key
  402. if source_check and callable(f):
  403. func_source_code = inspect.getsource(f)
  404. cache_hash.update(func_source_code.encode("utf-8"))
  405. cache_hash = str(cache_hash.hexdigest())
  406. cache_key = request.path + cache_hash
  407. return cache_key
  408. def _make_cache_key(args, kwargs, use_request) -> str:
  409. if query_string:
  410. return _make_cache_key_query_string()
  411. else:
  412. if callable(key_prefix):
  413. cache_key = key_prefix()
  414. elif "%s" in key_prefix:
  415. if use_request:
  416. cache_key = key_prefix % request.path
  417. else:
  418. cache_key = key_prefix % url_for(f.__name__, **kwargs)
  419. else:
  420. cache_key = key_prefix
  421. if source_check and callable(f):
  422. func_source_code = inspect.getsource(f)
  423. func_source_hash = hash_method(func_source_code.encode("utf-8"))
  424. func_source_hash = str(func_source_hash.hexdigest())
  425. cache_key += func_source_hash
  426. return cache_key
  427. decorated_function.uncached = f
  428. decorated_function.cache_timeout = timeout
  429. decorated_function.make_cache_key = default_make_cache_key
  430. return decorated_function
  431. return decorator
  432. def _memvname(self, funcname: str) -> str:
  433. return funcname + "_memver"
  434. def _memoize_make_version_hash(self) -> str:
  435. return base64.b64encode(uuid.uuid4().bytes)[:6].decode("utf-8")
  436. def _memoize_version(
  437. self,
  438. f: Callable,
  439. args: Optional[Any] = None,
  440. kwargs=None,
  441. reset: bool = False,
  442. delete: bool = False,
  443. timeout: Optional[int] = None,
  444. forced_update: Optional[Union[bool, Callable]] = False,
  445. args_to_ignore: Optional[Any] = None,
  446. ) -> Union[Tuple[str, str], Tuple[str, None]]:
  447. """Updates the hash version associated with a memoized function or
  448. method.
  449. """
  450. fname, instance_fname = function_namespace(f, args=args)
  451. version_key = self._memvname(fname)
  452. fetch_keys = [version_key]
  453. args_to_ignore = args_to_ignore or []
  454. if "self" in args_to_ignore:
  455. instance_fname = None
  456. if instance_fname:
  457. instance_version_key = self._memvname(instance_fname)
  458. fetch_keys.append(instance_version_key)
  459. # Only delete the per-instance version key or per-function version
  460. # key but not both.
  461. if delete:
  462. self.cache.delete_many(fetch_keys[-1])
  463. return fname, None
  464. version_data_list = list(self.cache.get_many(*fetch_keys))
  465. dirty = False
  466. if (
  467. callable(forced_update)
  468. and (
  469. forced_update(*(args or ()), **(kwargs or {}))
  470. if wants_args(forced_update)
  471. else forced_update()
  472. )
  473. is True
  474. ):
  475. # Mark key as dirty to update its TTL
  476. dirty = True
  477. if version_data_list[0] is None:
  478. version_data_list[0] = self._memoize_make_version_hash()
  479. dirty = True
  480. if instance_fname and version_data_list[1] is None:
  481. version_data_list[1] = self._memoize_make_version_hash()
  482. dirty = True
  483. # Only reset the per-instance version or the per-function version
  484. # but not both.
  485. if reset:
  486. fetch_keys = fetch_keys[-1:]
  487. version_data_list = [self._memoize_make_version_hash()]
  488. dirty = True
  489. if dirty:
  490. self.cache.set_many(
  491. dict(zip(fetch_keys, version_data_list)), timeout=timeout
  492. )
  493. return fname, "".join(version_data_list)
  494. def _memoize_make_cache_key(
  495. self,
  496. make_name: Optional[Callable] = None,
  497. timeout: Optional[Callable] = None,
  498. forced_update: bool = False,
  499. hash_method: Callable = hashlib.md5,
  500. source_check: Optional[bool] = False,
  501. args_to_ignore: Optional[Any] = None,
  502. ) -> Callable:
  503. """Function used to create the cache_key for memoized functions."""
  504. def make_cache_key(f, *args, **kwargs):
  505. _timeout = getattr(timeout, "cache_timeout", timeout)
  506. fname, version_data = self._memoize_version(
  507. f,
  508. args=args,
  509. kwargs=kwargs,
  510. timeout=_timeout,
  511. forced_update=forced_update,
  512. args_to_ignore=args_to_ignore,
  513. )
  514. #: this should have to be after version_data, so that it
  515. #: does not break the delete_memoized functionality.
  516. altfname = make_name(fname) if callable(make_name) else fname
  517. if callable(f):
  518. keyargs, keykwargs = self._memoize_kwargs_to_args(
  519. f, *args, **kwargs, args_to_ignore=args_to_ignore
  520. )
  521. else:
  522. keyargs, keykwargs = args, kwargs
  523. updated = f"{altfname}{keyargs}{keykwargs}"
  524. cache_key = hash_method()
  525. cache_key.update(updated.encode("utf-8"))
  526. # Use the source code if source_check is True and update the
  527. # cache_key with the function's source.
  528. if source_check and callable(f):
  529. func_source_code = inspect.getsource(f)
  530. cache_key.update(func_source_code.encode("utf-8"))
  531. cache_key = base64.b64encode(cache_key.digest())[:16]
  532. cache_key = cache_key.decode("utf-8")
  533. cache_key += version_data
  534. return cache_key
  535. return make_cache_key
  536. def _memoize_kwargs_to_args(self, f: Callable, *args, **kwargs) -> Any:
  537. #: Inspect the arguments to the function
  538. #: This allows the memoization to be the same
  539. #: whether the function was called with
  540. #: 1, b=2 is equivalent to a=1, b=2, etc.
  541. new_args = []
  542. arg_num = 0
  543. args_to_ignore = kwargs.pop("args_to_ignore", None) or []
  544. # If the function uses VAR_KEYWORD type of parameters,
  545. # we need to pass these further
  546. kw_keys_remaining = [key for key in kwargs.keys() if key not in args_to_ignore]
  547. arg_names = get_arg_names(f)
  548. args_len = len(arg_names)
  549. for i in range(args_len):
  550. arg_default = get_arg_default(f, i)
  551. if arg_names[i] in args_to_ignore:
  552. arg = None
  553. arg_num += 1
  554. elif i == 0 and arg_names[i] in ("self", "cls"):
  555. #: use the id func of the class instance
  556. #: this supports instance methods for
  557. #: the memoized functions, giving more
  558. #: flexibility to developers
  559. arg = get_id(args[0])
  560. arg_num += 1
  561. elif arg_names[i] in kwargs:
  562. arg = kwargs[arg_names[i]]
  563. kw_keys_remaining.pop(kw_keys_remaining.index(arg_names[i]))
  564. elif arg_num < len(args):
  565. arg = args[arg_num]
  566. arg_num += 1
  567. elif arg_default:
  568. arg = arg_default
  569. arg_num += 1
  570. else:
  571. arg = None
  572. arg_num += 1
  573. #: Attempt to convert all arguments to a
  574. #: hash/id or a representation?
  575. #: Not sure if this is necessary, since
  576. #: using objects as keys gets tricky quickly.
  577. # if hasattr(arg, '__class__'):
  578. # try:
  579. # arg = hash(arg)
  580. # except:
  581. # arg = get_id(arg)
  582. #: Or what about a special __cacherepr__ function
  583. #: on an object, this allows objects to act normal
  584. #: upon inspection, yet they can define a representation
  585. #: that can be used to make the object unique in the
  586. #: cache key. Given that a case comes across that
  587. #: an object "must" be used as a cache key
  588. # if hasattr(arg, '__cacherepr__'):
  589. # arg = arg.__cacherepr__
  590. new_args.append(arg)
  591. new_args.extend(args[len(arg_names) :])
  592. return (
  593. tuple(new_args),
  594. OrderedDict(
  595. sorted((k, v) for k, v in kwargs.items() if k in kw_keys_remaining)
  596. ),
  597. )
  598. def _bypass_cache(
  599. self, unless: Optional[Callable], f: Callable, *args, **kwargs
  600. ) -> bool:
  601. """Determines whether or not to bypass the cache by calling unless().
  602. Supports both unless() that takes in arguments and unless()
  603. that doesn't.
  604. """
  605. bypass_cache = False
  606. if callable(unless):
  607. argspec = inspect.getfullargspec(unless)
  608. has_args = len(argspec.args) > 0 or argspec.varargs or argspec.varkw
  609. # If unless() takes args, pass them in.
  610. if has_args:
  611. if unless(f, *args, **kwargs) is True:
  612. bypass_cache = True
  613. elif unless() is True:
  614. bypass_cache = True
  615. return bypass_cache
  616. def memoize(
  617. self,
  618. timeout: Optional[int] = None,
  619. make_name: Optional[Callable] = None,
  620. unless: Optional[Callable] = None,
  621. forced_update: Optional[Callable] = None,
  622. response_filter: Optional[Callable] = None,
  623. hash_method: Callable = hashlib.md5,
  624. cache_none: bool = False,
  625. source_check: Optional[bool] = None,
  626. args_to_ignore: Optional[Any] = None,
  627. ) -> Callable:
  628. """Use this to cache the result of a function, taking its arguments
  629. into account in the cache key.
  630. Information on
  631. `Memoization <http://en.wikipedia.org/wiki/Memoization>`_.
  632. Example::
  633. @cache.memoize(timeout=50)
  634. def big_foo(a, b):
  635. return a + b + random.randrange(0, 1000)
  636. .. code-block:: pycon
  637. >>> big_foo(5, 2)
  638. 753
  639. >>> big_foo(5, 3)
  640. 234
  641. >>> big_foo(5, 2)
  642. 753
  643. .. versionadded:: 0.4
  644. The returned decorated function now has three function attributes
  645. assigned to it.
  646. **uncached**
  647. The original undecorated function. readable only
  648. **cache_timeout**
  649. The cache timeout value for this function.
  650. For a custom value to take affect, this must be
  651. set before the function is called.
  652. readable and writable
  653. **make_cache_key**
  654. A function used in generating the cache_key used.
  655. readable and writable
  656. :param timeout: Default None. If set to an integer, will cache for that
  657. amount of time. Unit of time is in seconds.
  658. :param make_name: Default None. If set this is a function that accepts
  659. a single argument, the function name, and returns a
  660. new string to be used as the function name.
  661. If not set then the function name is used.
  662. :param unless: Default None. Cache will *always* execute the caching
  663. facilities unless this callable is true.
  664. This will bypass the caching entirely.
  665. :param forced_update: Default None. If this callable is true,
  666. cache value will be updated regardless cache
  667. is expired or not. Useful for background
  668. renewal of cached functions.
  669. :param response_filter: Default None. If not None, the callable is
  670. invoked after the cached funtion evaluation,
  671. and is given one arguement, the response
  672. content. If the callable returns False, the
  673. content will not be cached. Useful to prevent
  674. caching of code 500 responses.
  675. :param hash_method: Default hashlib.md5. The hash method used to
  676. generate the keys for cached results.
  677. :param cache_none: Default False. If set to True, add a key exists
  678. check when cache.get returns None. This will likely
  679. lead to wrongly returned None values in concurrent
  680. situations and is not recommended to use.
  681. :param source_check: Default None. If None will use the value set by
  682. CACHE_SOURCE_CHECK.
  683. If True, include the function's source code in the
  684. hash to avoid using cached values when the source
  685. code has changed and the input values remain the
  686. same. This ensures that the cache_key will be
  687. formed with the function's source code hash in
  688. addition to other parameters that may be included
  689. in the formation of the key.
  690. :param args_to_ignore: List of arguments that will be ignored while
  691. generating the cache key. Default to None.
  692. This means that those arguments may change
  693. without affecting the cache value that will be
  694. returned.
  695. .. versionadded:: 0.5
  696. params ``make_name``, ``unless``
  697. .. versionadded:: 1.10
  698. params ``args_to_ignore``
  699. """
  700. def memoize(f):
  701. @functools.wraps(f)
  702. def decorated_function(*args, **kwargs):
  703. #: bypass cache
  704. if self._bypass_cache(unless, f, *args, **kwargs):
  705. return self._call_fn(f, *args, **kwargs)
  706. nonlocal source_check
  707. if source_check is None:
  708. source_check = self.source_check
  709. try:
  710. cache_key = decorated_function.make_cache_key(f, *args, **kwargs)
  711. if (
  712. callable(forced_update)
  713. and (
  714. forced_update(*args, **kwargs)
  715. if wants_args(forced_update)
  716. else forced_update()
  717. )
  718. is True
  719. ):
  720. rv = None
  721. found = False
  722. else:
  723. rv = self.cache.get(cache_key)
  724. found = True
  725. # If the value returned by cache.get() is None, it
  726. # might be because the key is not found in the cache
  727. # or because the cached value is actually None
  728. if rv is None:
  729. # If we're sure we don't need to cache None values
  730. # (cache_none=False), don't bother checking for
  731. # key existence, as it can lead to false positives
  732. # if a concurrent call already cached the
  733. # key between steps. This would cause us to
  734. # return None when we shouldn't
  735. if not cache_none:
  736. found = False
  737. else:
  738. found = self.cache.has(cache_key)
  739. except Exception:
  740. if self.app.debug:
  741. raise
  742. logger.exception("Exception possibly due to cache backend.")
  743. return self._call_fn(f, *args, **kwargs)
  744. if not found:
  745. rv = self._call_fn(f, *args, **kwargs)
  746. if inspect.isgenerator(rv):
  747. rv = [val for val in rv]
  748. if response_filter is None or response_filter(rv):
  749. try:
  750. self.cache.set(
  751. cache_key,
  752. rv,
  753. timeout=decorated_function.cache_timeout,
  754. )
  755. except Exception:
  756. if self.app.debug:
  757. raise
  758. logger.exception("Exception possibly due to cache backend.")
  759. return rv
  760. decorated_function.uncached = f
  761. decorated_function.cache_timeout = timeout
  762. decorated_function.make_cache_key = self._memoize_make_cache_key(
  763. make_name=make_name,
  764. timeout=decorated_function,
  765. forced_update=forced_update,
  766. hash_method=hash_method,
  767. source_check=source_check,
  768. args_to_ignore=args_to_ignore,
  769. )
  770. decorated_function.delete_memoized = lambda: self.delete_memoized(f)
  771. return decorated_function
  772. return memoize
  773. def delete_memoized(self, f, *args, **kwargs) -> None:
  774. """Deletes the specified functions caches, based by given parameters.
  775. If parameters are given, only the functions that were memoized
  776. with them will be erased. Otherwise all versions of the caches
  777. will be forgotten.
  778. Example::
  779. @cache.memoize(50)
  780. def random_func():
  781. return random.randrange(1, 50)
  782. @cache.memoize()
  783. def param_func(a, b):
  784. return a+b+random.randrange(1, 50)
  785. .. code-block:: pycon
  786. >>> random_func()
  787. 43
  788. >>> random_func()
  789. 43
  790. >>> cache.delete_memoized(random_func)
  791. >>> random_func()
  792. 16
  793. >>> param_func(1, 2)
  794. 32
  795. >>> param_func(1, 2)
  796. 32
  797. >>> param_func(2, 2)
  798. 47
  799. >>> cache.delete_memoized(param_func, 1, 2)
  800. >>> param_func(1, 2)
  801. 13
  802. >>> param_func(2, 2)
  803. 47
  804. Delete memoized is also smart about instance methods vs class methods.
  805. When passing a instancemethod, it will only clear the cache related
  806. to that instance of that object. (object uniqueness can be overridden
  807. by defining the __repr__ method, such as user id).
  808. When passing a classmethod, it will clear all caches related across
  809. all instances of that class.
  810. Example::
  811. class Adder(object):
  812. @cache.memoize()
  813. def add(self, b):
  814. return b + random.random()
  815. .. code-block:: pycon
  816. >>> adder1 = Adder()
  817. >>> adder2 = Adder()
  818. >>> adder1.add(3)
  819. 3.23214234
  820. >>> adder2.add(3)
  821. 3.60898509
  822. >>> cache.delete_memoized(adder1.add)
  823. >>> adder1.add(3)
  824. 3.01348673
  825. >>> adder2.add(3)
  826. 3.60898509
  827. >>> cache.delete_memoized(Adder.add)
  828. >>> adder1.add(3)
  829. 3.53235667
  830. >>> adder2.add(3)
  831. 3.72341788
  832. :param fname: The memoized function.
  833. :param \\*args: A list of positional parameters used with
  834. memoized function.
  835. :param \\**kwargs: A dict of named parameters used with
  836. memoized function.
  837. .. note::
  838. Flask-Caching uses inspect to order kwargs into positional args when
  839. the function is memoized. If you pass a function reference into
  840. ``fname``, Flask-Caching will be able to place the args/kwargs in
  841. the proper order, and delete the positional cache.
  842. However, if ``delete_memoized`` is just called with the name of the
  843. function, be sure to pass in potential arguments in the same order
  844. as defined in your function as args only, otherwise Flask-Caching
  845. will not be able to compute the same cache key and delete all
  846. memoized versions of it.
  847. .. note::
  848. Flask-Caching maintains an internal random version hash for
  849. the function. Using delete_memoized will only swap out
  850. the version hash, causing the memoize function to recompute
  851. results and put them into another key.
  852. This leaves any computed caches for this memoized function within
  853. the caching backend.
  854. It is recommended to use a very high timeout with memoize if using
  855. this function, so that when the version hash is swapped, the old
  856. cached results would eventually be reclaimed by the caching
  857. backend.
  858. """
  859. if not callable(f):
  860. raise TypeError(
  861. "Deleting messages by relative name is not supported, please "
  862. "use a function reference."
  863. )
  864. if not (args or kwargs):
  865. self._memoize_version(f, reset=True)
  866. else:
  867. cache_key = f.make_cache_key(f.uncached, *args, **kwargs)
  868. self.cache.delete(cache_key)
  869. def delete_memoized_verhash(self, f: Callable, *args) -> None:
  870. """Delete the version hash associated with the function.
  871. .. warning::
  872. Performing this operation could leave keys behind that have
  873. been created with this version hash. It is up to the application
  874. to make sure that all keys that may have been created with this
  875. version hash at least have timeouts so they will not sit orphaned
  876. in the cache backend.
  877. """
  878. if not callable(f):
  879. raise TypeError(
  880. "Deleting messages by relative name is not supported, please"
  881. "use a function reference."
  882. )
  883. self._memoize_version(f, delete=True)