Debian Codegraph @

django/core/cache/backends/filebased.py

raw
1 "File-based cache backend"
2
3 import glob
4 import os
5 import pickle
6 import random
7 import tempfile
8 import time
9 import zlib
10 from hashlib import md5
11
12 from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
13 from django.core.files import locks
14 from django.core.files.move import file_move_safe
15 from django.utils._os import safe_makedirs
16
17
18 class FileBasedCache(BaseCache):
19 cache_suffix = ".djcache"
20 pickle_protocol = pickle.HIGHEST_PROTOCOL
21
22 def __init__(self, dir, params):
23 super().__init__(params)
24 self._dir = os.path.abspath(dir)
25 self._createdir()
26
27 def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
28 if self.has_key(key, version):
29 return False
30 self.set(key, value, timeout, version)
31 return True
32
33 def get(self, key, default=None, version=None):
34 fname = self._key_to_file(key, version)
35 try:
36 with open(fname, "rb") as f:
37 if not self._is_expired(f):
38 return pickle.loads(zlib.decompress(f.read()))
39 except FileNotFoundError:
40 pass
41 return default
42
43 def _write_content(self, file, timeout, value):
44 expiry = self.get_backend_timeout(timeout)
45 file.write(pickle.dumps(expiry, self.pickle_protocol))
46 file.write(zlib.compress(pickle.dumps(value, self.pickle_protocol)))
47
48 def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
49 self._createdir() # Cache dir can be deleted at any time.
50 fname = self._key_to_file(key, version)
51 self._cull() # make some room if necessary
52 fd, tmp_path = tempfile.mkstemp(dir=self._dir)
53 renamed = False
54 try:
55 with open(fd, "wb") as f:
56 self._write_content(f, timeout, value)
57 file_move_safe(tmp_path, fname, allow_overwrite=True)
58 renamed = True
59 finally:
60 if not renamed:
61 os.remove(tmp_path)
62
63 def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None):
64 try:
65 with open(self._key_to_file(key, version), "r+b") as f:
66 try:
67 locks.lock(f, locks.LOCK_EX)
68 if self._is_expired(f):
69 return False
70 else:
71 previous_value = pickle.loads(zlib.decompress(f.read()))
72 f.seek(0)
73 self._write_content(f, timeout, previous_value)
74 return True
75 finally:
76 locks.unlock(f)
77 except FileNotFoundError:
78 return False
79
80 def delete(self, key, version=None):
81 return self._delete(self._key_to_file(key, version))
82
83 def _delete(self, fname):
84 if not fname.startswith(self._dir) or not os.path.exists(fname):
85 return False
86 try:
87 os.remove(fname)
88 except FileNotFoundError:
89 # The file may have been removed by another process.
90 return False
91 return True
92
93 def has_key(self, key, version=None):
94 fname = self._key_to_file(key, version)
95 try:
96 with open(fname, "rb") as f:
97 return not self._is_expired(f)
98 except FileNotFoundError:
99 return False
100
101 def _cull(self):
102 """
103 Remove random cache entries if max_entries is reached at a ratio
104 of num_entries / cull_frequency. A value of 0 for CULL_FREQUENCY means
105 that the entire cache will be purged.
106 """
107 filelist = self._list_cache_files()
108 num_entries = len(filelist)
109 if num_entries < self._max_entries:
110 return # return early if no culling is required
111 if self._cull_frequency == 0:
112 return self.clear() # Clear the cache when CULL_FREQUENCY = 0
113 # Delete a random selection of entries
114 filelist = random.sample(filelist, int(num_entries / self._cull_frequency))
115 for fname in filelist:
116 self._delete(fname)
117
118 def _createdir(self):
119 # Workaround because os.makedirs() doesn't apply the "mode" argument
120 # to intermediate-level directories.
121 # https://github.com/python/cpython/issues/86533
122 safe_makedirs(self._dir, mode=0o700, exist_ok=True)
123
124 def _key_to_file(self, key, version=None):
125 """
126 Convert a key into a cache file path. Basically this is the
127 root cache path joined with the md5sum of the key and a suffix.
128 """
129 key = self.make_and_validate_key(key, version=version)
130 return os.path.join(
131 self._dir,
132 "".join(
133 [
134 md5(key.encode(), usedforsecurity=False).hexdigest(),
135 self.cache_suffix,
136 ]
137 ),
138 )
139
140 def clear(self):
141 """
142 Remove all the cache files.
143 """
144 for fname in self._list_cache_files():
145 self._delete(fname)
146
147 def _is_expired(self, f):
148 """
149 Take an open cache file `f` and delete it if it's expired.
150 """
151 try:
152 exp = pickle.load(f)
153 except EOFError:
154 exp = 0 # An empty file is considered expired.
155 if exp is not None and exp < time.time():
156 f.close() # On Windows a file has to be closed before deleting
157 self._delete(f.name)
158 return True
159 return False
160
161 def _list_cache_files(self):
162 """
163 Get a list of paths to all the cache files. These are all the files
164 in the root cache dir that end on the cache_suffix.
165 """
166 return [
167 os.path.join(self._dir, fname)
168 for fname in glob.glob(f"*{self.cache_suffix}", root_dir=self._dir)
169 ]