Skip to content

Commit

Permalink
Fix incorrect location of redirects.sqlite when using filesystem backend
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Aug 23, 2021
1 parent 8985753 commit d7adfad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Expand Up @@ -45,6 +45,9 @@
* Add aliases for previous names for backwards-compatibility

-----
### 0.7.5 (2021-TBD)
* Fix incorrect location of `redirects.sqlite` when using filesystem backend

### 0.7.4 (2021-08-16)
* Fix an issue with httpdate strings from `Expires` headers not getting converted to UTC
* Fix a packaging issue with extra files added to top-level wheel directory
Expand Down
16 changes: 12 additions & 4 deletions requests_cache/backends/filesystem.py
Expand Up @@ -38,7 +38,7 @@
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
Expand All @@ -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"""
Expand Down Expand Up @@ -126,8 +132,7 @@ def __setitem__(self, key, value):
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))
Expand All @@ -137,6 +142,9 @@ def clear(self):
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('*'))
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/base_cache_test.py
Expand Up @@ -52,6 +52,10 @@ def init_session(self, clear=True, **kwargs) -> CachedSession:

return CachedSession(backend=backend, **self.init_kwargs, **kwargs)

@classmethod
def teardown_class(cls):
cls().init_session(clear=True)

@pytest.mark.parametrize('serializer', TEST_SERIALIZERS.values())
@pytest.mark.parametrize('method', HTTPBIN_METHODS)
@pytest.mark.parametrize('field', ['params', 'data', 'json'])
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_filesystem.py
@@ -1,5 +1,5 @@
import pickle
from os.path import isfile
from os.path import dirname, isfile
from shutil import rmtree
from tempfile import gettempdir

Expand Down Expand Up @@ -57,3 +57,6 @@ def test_paths(self, serializer_name):
for path in session.cache.paths():
assert isfile(path)
assert path.endswith(f'.{expected_extension}')

# Redirects db should be in the same directory as response files
assert dirname(session.cache.redirects.db_path) == session.cache.responses.cache_dir

0 comments on commit d7adfad

Please sign in to comment.