Debian Codegraph @

src/flask_caching/backends/simplecache.py

raw
1 """
2 flask_caching.backends.simple
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 The simple cache backend.
6
7 :copyright: (c) 2018 by Peter Justin.
8 :copyright: (c) 2010 by Thadeus Burgess.
9 :license: BSD, see LICENSE for more details.
10 """
11
12 import logging
13 import warnings
14 from typing import Any
15
16 from cachelib import SimpleCache as CachelibSimpleCache
17 from flask import Flask
18
19 from flask_caching.backends.base import BaseCache
20
21 logger = logging.getLogger(__name__)
22
23
24 class SimpleCache(BaseCache, CachelibSimpleCache):
25 """Simple memory cache for single process environments. All operations
26 are protected by a :class:`threading.RLock`, making a cache instance safe
27 to use from multiple threads within the same process.
28
29 :param threshold: the maximum number of items the cache stores before
30 it starts deleting some.
31 :param default_timeout: the default timeout that is used if no timeout is
32 specified on ``set``. A timeout of
33 0 indicates that the cache never expires.
34 :param ignore_delete_many_errors: If set to ``False`` the ``delete_many``
35 method raises a ``RuntimeError`` in case
36 a key couldn't be deleted.
37 Defaults to ``False``.
38 :param ignore_errors: Deprecated alias for ``ignore_delete_many_errors``.
39
40 .. deprecated:: 2.5.0
41 Will be removed in the next version.
42 """
43
44 def __init__(
45 self,
46 threshold: int = 500,
47 default_timeout: int = 300,
48 ignore_delete_many_errors: bool = False,
49 ignore_errors: bool | None = None,
50 ) -> None:
51 if ignore_errors is not None:
52 warnings.warn(
53 "'ignore_errors' is deprecated and will be removed in the next "
54 "version. Use 'ignore_delete_many_errors' instead.",
55 DeprecationWarning,
56 stacklevel=2,
57 )
58 ignore_delete_many_errors = ignore_errors
59
60 CachelibSimpleCache.__init__(
61 self,
62 threshold=threshold,
63 default_timeout=default_timeout,
64 ignore_delete_many_errors=ignore_delete_many_errors,
65 )
66
67 @classmethod
68 def factory(
69 cls,
70 app: Flask,
71 config: dict[str, Any],
72 args: list[Any],
73 kwargs: dict[str, Any],
74 ) -> "SimpleCache":
75 kwargs.update(dict(threshold=config["CACHE_THRESHOLD"]))
76 return cls(*args, **kwargs)