summaryrefslogtreecommitdiff
path: root/requests_cache/backends
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-08-23 11:32:12 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-08-23 11:32:13 -0500
commitd7adfad199419de0ecfc3d4401c7205b2e9c9a85 (patch)
tree3a1cb019e3ec5e69bcd54ebd4e8299641134bccc /requests_cache/backends
parent8985753310d099489ad7a5a0f33253ccaed11d9b (diff)
downloadrequests-cache-d7adfad199419de0ecfc3d4401c7205b2e9c9a85.tar.gz
Fix incorrect location of redirects.sqlite when using filesystem backend
Diffstat (limited to 'requests_cache/backends')
-rw-r--r--requests_cache/backends/filesystem.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/requests_cache/backends/filesystem.py b/requests_cache/backends/filesystem.py
index a70a6f7..44d0470 100644
--- a/requests_cache/backends/filesystem.py
+++ b/requests_cache/backends/filesystem.py
@@ -38,7 +38,7 @@ API Reference
from contextlib import contextmanager
from glob import glob
from os import listdir, makedirs, unlink
-from os.path import basename, dirname, join, splitext
+from os.path import basename, join, splitext
from pathlib import Path
from pickle import PickleError
from shutil import rmtree
@@ -64,13 +64,19 @@ class FileCache(BaseCache):
def __init__(self, cache_name: Union[Path, str] = 'http_cache', use_temp: bool = False, **kwargs):
super().__init__(**kwargs)
self.responses = FileDict(cache_name, use_temp=use_temp, **kwargs)
- db_path = join(dirname(self.responses.cache_dir), 'redirects.sqlite')
+ db_path = join(self.responses.cache_dir, 'redirects.sqlite')
self.redirects = SQLiteDict(db_path, 'redirects', **kwargs)
def paths(self) -> List[str]:
"""Get absolute file paths to all cached responses"""
return self.responses.paths()
+ def clear(self):
+ """Clear the cache"""
+ # FileDict.clear() removes and re-creates the cache directory, including redirects.sqlite
+ self.responses.clear()
+ self.redirects.init_db()
+
class FileDict(BaseStorage):
"""A dictionary-like interface to files on the local filesystem"""
@@ -126,8 +132,7 @@ class FileDict(BaseStorage):
f.write(self.serializer.dumps(value))
def __iter__(self):
- for path in self.paths():
- yield splitext(basename(path))[0]
+ yield from self.keys()
def __len__(self):
return len(listdir(self.cache_dir))
@@ -137,6 +142,9 @@ class FileDict(BaseStorage):
rmtree(self.cache_dir, ignore_errors=True)
makedirs(self.cache_dir)
+ def keys(self):
+ return [splitext(basename(path))[0] for path in self.paths()]
+
def paths(self) -> List[str]:
"""Get absolute file paths to all cached responses"""
return glob(self._path('*'))