summaryrefslogtreecommitdiff
path: root/src/pip/_vendor/cachecontrol/caches/file_cache.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/pip/_vendor/cachecontrol/caches/file_cache.py')
-rw-r--r--src/pip/_vendor/cachecontrol/caches/file_cache.py58
1 files changed, 50 insertions, 8 deletions
diff --git a/src/pip/_vendor/cachecontrol/caches/file_cache.py b/src/pip/_vendor/cachecontrol/caches/file_cache.py
index 607b94524..f1ddb2ebd 100644
--- a/src/pip/_vendor/cachecontrol/caches/file_cache.py
+++ b/src/pip/_vendor/cachecontrol/caches/file_cache.py
@@ -1,8 +1,12 @@
+# SPDX-FileCopyrightText: 2015 Eric Larson
+#
+# SPDX-License-Identifier: Apache-2.0
+
import hashlib
import os
from textwrap import dedent
-from ..cache import BaseCache
+from ..cache import BaseCache, SeparateBodyBaseCache
from ..controller import CacheController
try:
@@ -53,7 +57,8 @@ def _secure_open_write(filename, fmode):
raise
-class FileCache(BaseCache):
+class _FileCacheMixin:
+ """Shared implementation for both FileCache variants."""
def __init__(
self,
@@ -114,22 +119,27 @@ class FileCache(BaseCache):
except FileNotFoundError:
return None
- def set(self, key, value):
+ def set(self, key, value, expires=None):
name = self._fn(key)
+ self._write(name, value)
+ def _write(self, path, data: bytes):
+ """
+ Safely write the data to the given path.
+ """
# Make sure the directory exists
try:
- os.makedirs(os.path.dirname(name), self.dirmode)
+ os.makedirs(os.path.dirname(path), self.dirmode)
except (IOError, OSError):
pass
- with self.lock_class(name) as lock:
+ with self.lock_class(path) as lock:
# Write our actual file
with _secure_open_write(lock.path, self.filemode) as fh:
- fh.write(value)
+ fh.write(data)
- def delete(self, key):
- name = self._fn(key)
+ def _delete(self, key, suffix):
+ name = self._fn(key) + suffix
if not self.forever:
try:
os.remove(name)
@@ -137,6 +147,38 @@ class FileCache(BaseCache):
pass
+class FileCache(_FileCacheMixin, BaseCache):
+ """
+ Traditional FileCache: body is stored in memory, so not suitable for large
+ downloads.
+ """
+
+ def delete(self, key):
+ self._delete(key, "")
+
+
+class SeparateBodyFileCache(_FileCacheMixin, SeparateBodyBaseCache):
+ """
+ Memory-efficient FileCache: body is stored in a separate file, reducing
+ peak memory usage.
+ """
+
+ def get_body(self, key):
+ name = self._fn(key) + ".body"
+ try:
+ return open(name, "rb")
+ except FileNotFoundError:
+ return None
+
+ def set_body(self, key, body):
+ name = self._fn(key) + ".body"
+ self._write(name, body)
+
+ def delete(self, key):
+ self._delete(key, "")
+ self._delete(key, ".body")
+
+
def url_to_file_path(url, filecache):
"""Return the file cache path based on the URL.