7from textwrap
import dedent
9from ..cache
import BaseCache, SeparateBodyBaseCache
10from ..controller
import CacheController
16 FileNotFoundError = (IOError, OSError)
42 except (IOError, OSError):
50 fd =
os.open(filename, flags, fmode)
61 """Shared implementation for both FileCache variants."""
73 if use_dir_lock
is not None and lock_class
is not None:
74 raise ValueError(
"Cannot use use_dir_lock and lock_class together")
77 from lockfile
import LockFile
82 NOTE: In order to use the FileCache you must have
83 lockfile installed. You can install it via pip:
87 raise ImportError(notice)
91 lock_class = MkdirLockFile
93 elif lock_class
is None:
109 hashed = self.
encode(name)
110 parts = list(hashed[:5]) + [hashed]
116 with open(name,
"rb")
as fh:
119 except FileNotFoundError:
122 def set(self, key, value, expires=None):
128 Safely write the data to the given path.
133 except (IOError, OSError):
142 name = self.
_fn(key) + suffix
146 except FileNotFoundError:
152 Traditional FileCache: body is stored in memory, so not suitable for large
156 def delete(self, key):
162 Memory-efficient FileCache: body is stored in a separate file, reducing
167 name = self.
_fn(key) +
".body"
169 return open(name,
"rb")
170 except FileNotFoundError:
174 name = self.
_fn(key) +
".body"
177 def delete(self, key):
183 """Return the file cache path based on the URL.
185 This does not ensure the file exists!
set_body(self, key, body)
_write(self, path, bytes data)
_delete(self, key, suffix)
set(self, key, value, expires=None)
__init__(self, directory, forever=False, filemode=0o0600, dirmode=0o0700, use_dir_lock=None, lock_class=None)
_secure_open_write(filename, fmode)
url_to_file_path(url, filecache)