| 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 |
|
| 14 |
from cachelib import SimpleCache as CachelibSimpleCache |
| 15 |
|
| 16 |
from flask_caching.backends.base import BaseCache |
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
class SimpleCache(BaseCache, CachelibSimpleCache): |
| 22 |
"""Simple memory cache for single process environments. This class exists |
| 23 |
mainly for the development server and is not 100% thread safe. It tries |
| 24 |
to use as many atomic operations as possible and no locks for simplicity |
| 25 |
but it could happen under heavy load that keys are added multiple times. |
| 26 |
|
| 27 |
:param threshold: the maximum number of items the cache stores before |
| 28 |
it starts deleting some. |
| 29 |
:param default_timeout: the default timeout that is used if no timeout is |
| 30 |
specified on :meth:`~BaseCache.set`. A timeout of |
| 31 |
0 indicates that the cache never expires. |
| 32 |
:param ignore_errors: If set to ``True`` the :meth:`~BaseCache.delete_many` |
| 33 |
method will ignore any errors that occurred during |
| 34 |
the deletion process. However, if it is set to |
| 35 |
``False`` it will stop on the first error. Defaults |
| 36 |
to ``False``. |
| 37 |
""" |
| 38 |
|
| 39 |
def __init__(self, threshold=500, default_timeout=300, ignore_errors=False): |
| 40 |
BaseCache.__init__(self, default_timeout=default_timeout) |
| 41 |
CachelibSimpleCache.__init__( |
| 42 |
self, threshold=threshold, default_timeout=default_timeout |
| 43 |
) |
| 44 |
|
| 45 |
self.ignore_errors = ignore_errors |
| 46 |
|
| 47 |
@classmethod |
| 48 |
def factory(cls, app, config, args, kwargs): |
| 49 |
kwargs.update( |
| 50 |
dict( |
| 51 |
threshold=config["CACHE_THRESHOLD"], |
| 52 |
ignore_errors=config["CACHE_IGNORE_ERRORS"], |
| 53 |
) |
| 54 |
) |
| 55 |
return cls(*args, **kwargs) |