From be8e9e9c8347b7707ecb16f7a6ff84f22070a4f6 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Tue, 10 Aug 2021 12:40:32 -0500 Subject: SQLite clear(): first attempt DROP TABLE, then delete and re-initialize the cache file if that fails --- requests_cache/backends/sqlite.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'requests_cache/backends/sqlite.py') diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py index d6fef95..9fe59cb 100644 --- a/requests_cache/backends/sqlite.py +++ b/requests_cache/backends/sqlite.py @@ -49,13 +49,17 @@ class DbCache(BaseCache): self.redirects.vacuum() def clear(self): - """Clear the cache by deleting the cache file and re-initializing. This is done to allow - clear() to succeed even if the file is corrupted. + """Clear the cache. If this fails due to a corrupted cache or other I/O error, this will + attempt to delete the cache file and re-initialize. """ - if isfile(self.responses.db_path): - unlink(self.responses.db_path) - self.responses.init_db() - self.redirects.init_db() + try: + super().clear() + except Exception: + logger.exception('Failed to clear cache') + if isfile(self.responses.db_path): + unlink(self.responses.db_path) + self.responses.init_db() + self.redirects.init_db() class DbDict(BaseStorage): -- cgit v1.2.1