Debian Codegraph @

src/flask_caching/backends/filesystemcache.py

raw
1 """
2 flask_caching.backends.filesystem
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 The filesystem caching 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 hashlib
13 import logging
14
15 from cachelib import FileSystemCache as CachelibFileSystemCache
16
17 from flask_caching.backends.base import BaseCache
18
19 logger = logging.getLogger(__name__)
20
21
22 class FileSystemCache(BaseCache, CachelibFileSystemCache):
23 """A cache that stores the items on the file system. This cache depends
24 on being the only user of the `cache_dir`. Make absolutely sure that
25 nobody but this cache stores files there or otherwise the cache will
26 randomly delete files therein.
27
28 :param cache_dir: the directory where cache files are stored.
29 :param threshold: the maximum number of items the cache stores before
30 it starts deleting some. A threshold value of 0
31 indicates no threshold.
32 :param default_timeout: the default timeout that is used if no timeout is
33 specified on :meth:`~BaseCache.set`. A timeout of
34 0 indicates that the cache never expires.
35 :param mode: the file mode wanted for the cache files, default 0600
36 :param hash_method: Default hashlib.md5. The hash method used to
37 generate the filename for cached results.
38 :param ignore_errors: If set to ``True`` the :meth:`~BaseCache.delete_many`
39 method will ignore any errors that occurred during the
40 deletion process. However, if it is set to ``False``
41 it will stop on the first error. Defaults to
42 ``False``.
43 """
44
45 def __init__(
46 self,
47 cache_dir,
48 threshold=500,
49 default_timeout=300,
50 mode=0o600,
51 hash_method=hashlib.md5,
52 ignore_errors=False,
53 ):
54 BaseCache.__init__(self, default_timeout=default_timeout)
55 CachelibFileSystemCache.__init__(
56 self,
57 cache_dir=cache_dir,
58 threshold=threshold,
59 default_timeout=default_timeout,
60 mode=mode,
61 hash_method=hash_method,
62 )
63
64 self.ignore_errors = ignore_errors
65
66 @classmethod
67 def factory(cls, app, config, args, kwargs):
68 args.insert(0, config["CACHE_DIR"])
69 kwargs.update(
70 dict(
71 threshold=config["CACHE_THRESHOLD"],
72 ignore_errors=config["CACHE_IGNORE_ERRORS"],
73 )
74 )
75 return cls(*args, **kwargs)