MCPcopy Create free account
hub / github.com/requests-cache/requests-cache / SQLiteCache

Class SQLiteCache

requests_cache/backends/sqlite.py:34–190  ·  view source on GitHub ↗

SQLite cache backend. Args: db_path: Database file path use_cache_dir: Store database in a user cache directory (e.g., `~/.cache/http_cache.sqlite`) use_temp: Store database in a temp directory (e.g., ``/tmp/http_cache.sqlite``) use_memory: Store database in memo

Source from the content-addressed store, hash-verified

32
33
34class SQLiteCache(BaseCache):
35 """SQLite cache backend.
36
37 Args:
38 db_path: Database file path
39 use_cache_dir: Store database in a user cache directory (e.g., `~/.cache/http_cache.sqlite`)
40 use_temp: Store database in a temp directory (e.g., ``/tmp/http_cache.sqlite``)
41 use_memory: Store database in memory instead of in a file
42 busy_timeout: Timeout in milliseconds for SQLite to wait if a table is locked.
43 See `pragma: busy_timeout <https://www.sqlite.org/pragma.html#pragma_busy_timeout>`_
44 for details.
45 fast_save: Significantly increases cache write performance, but with the possibility of data
46 loss. See `pragma: synchronous <https://www.sqlite.org/pragma.html#pragma_synchronous>`_
47 for details.
48 wal: Use `Write Ahead Logging <https://sqlite.org/wal.html>`_, so readers do not block writers.
49 kwargs: Additional keyword arguments for :py:func:`sqlite3.connect`
50 """
51
52 def __init__(
53 self,
54 db_path: StrOrPath = 'http_cache',
55 serializer: Optional[SerializerType] = None,
56 **kwargs,
57 ):
58 super().__init__(cache_name=str(db_path), **kwargs)
59 # Only override serializer if a non-None value is specified
60 skwargs = {'serializer': serializer, **kwargs} if serializer else kwargs
61 self.responses: SQLiteDict = SQLiteDict(db_path, table_name='responses', **skwargs)
62 self.redirects: SQLiteDict = SQLiteDict(
63 db_path,
64 table_name='redirects',
65 lock=self.responses._lock,
66 serializer=None,
67 **kwargs,
68 )
69
70 @property
71 def db_path(self) -> StrOrPath:
72 return self.responses.db_path
73
74 def clear(self):
75 """Delete all items from the cache. If this fails due to a corrupted cache or other I/O
76 error, this will attempt to delete the cache file and re-initialize.
77 """
78 try:
79 super().clear()
80 except Exception:
81 logger.exception('Failed to clear cache')
82 if isfile(self.responses.db_path):
83 unlink(self.responses.db_path)
84 self.responses.init_db()
85 self.redirects.init_db()
86
87 # A more efficient SQLite implementation of BaseCache.delete
88 def delete(
89 self,
90 *keys: str,
91 expired: bool = False,

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…