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

Method delete

requests_cache/backends/sqlite.py:88–124  ·  view source on GitHub ↗

Remove responses from the cache according one or more conditions. Args: keys: Remove responses with these cache keys expired: Remove all expired responses vacuum: Vacuum the database after deleting responses to free up disk space kwargs: Addit

(
        self,
        *keys: str,
        expired: bool = False,
        vacuum: bool = True,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

86
87 # A more efficient SQLite implementation of BaseCache.delete
88 def delete(
89 self,
90 *keys: str,
91 expired: bool = False,
92 vacuum: bool = True,
93 **kwargs,
94 ):
95 """Remove responses from the cache according one or more conditions.
96
97 Args:
98 keys: Remove responses with these cache keys
99 expired: Remove all expired responses
100 vacuum: Vacuum the database after deleting responses to free up disk space
101 kwargs: Additional keyword arguments for :py:meth:`BaseCache.delete`
102 """
103 # If deleting a single key, skip bulk delete + vacuum and ignore any KeyErrors
104 if len(keys) == 1:
105 try:
106 del self.responses[keys[0]]
107 except KeyError:
108 pass
109 # Bulk delete multiple keys and/or all expired responses in SQL
110 elif keys:
111 self.responses.bulk_delete(keys)
112 if expired:
113 self._delete_expired()
114
115 # For any remaining conditions, use base implementation
116 if kwargs:
117 with self.responses._lock:
118 return super().delete(**kwargs)
119 else:
120 self._prune_redirects()
121
122 # Skip vacuuming if only one key was deleted, or if explicitly disabled
123 if vacuum and (expired or kwargs or len(keys) > 1):
124 self.responses.vacuum()
125
126 def _delete_expired(self):
127 """A more efficient implementation of deleting expired responses in SQL"""

Callers

nothing calls this directly

Calls 4

_delete_expiredMethod · 0.95
_prune_redirectsMethod · 0.95
vacuumMethod · 0.80
bulk_deleteMethod · 0.45

Tested by

no test coverage detected