SQLite#

SQLite cache backend. For usage details, see Backends: SQLite.

SQLiteCache

SQLite cache backend.

SQLiteDict

A dictionary-like interface for SQLite

class requests_cache.backends.sqlite.SQLiteCache(db_path='http_cache', serializer=None, **kwargs)[source]#

Bases: BaseCache

SQLite cache backend.

Parameters:
  • db_path (Union[Path, str]) – Database file path

  • use_cache_dir – Store datebase 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 memory instead of in a file

  • busy_timeout – Timeout in milliseconds for SQLite to wait if a table is locked. See pragma: busy_timeout for details.

  • fast_save – Significantly increases cache write performance, but with the possibility of data loss. See pragma: synchronous for details.

  • wal – Use Write Ahead Logging, so readers do not block writers.

  • kwargs – Additional keyword arguments for sqlite3.connect()

  • serializer (Union[str, SerializerPipeline, Stage, None]) –

clear()[source]#

Delete all items from 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.

close()#

Close any open backend connections

contains(key=None, request=None, url=None)#

Check if the specified request is cached

Parameters:
count(expired=True)[source]#

Count number of responses, optionally excluding expired

Parameters:

expired (bool) – Set to False to count only unexpired responses

Return type:

int

create_key(request, match_headers=None, **kwargs)#

Create a normalized cache key from a request object

Parameters:
Return type:

str

property db_path: Union[Path, str]#
delete(*keys, expired=False, vacuum=True, **kwargs)[source]#

Remove responses from the cache according one or more conditions.

Parameters:
  • keys (str) – Remove responses with these cache keys

  • expired (bool) – Remove all expired responses

  • vacuum (bool) – Vacuum the database after deleting responses to free up disk space

  • kwargs – Additional keyword arguments for BaseCache.delete()

filter(valid=True, expired=True, invalid=False, older_than=None)[source]#

Get responses from the cache, with optional filters for which responses to include:

Parameters:
  • valid (bool) – Include valid and unexpired responses; set to False to get only expired/invalid/old responses

  • expired (bool) – Include expired responses

  • invalid (bool) – Include invalid responses (as an empty CachedResponse)

  • older_than (Union[None, int, float, str, datetime, timedelta]) – Get responses older than this value, relative to response.created_at

Return type:

Iterator[CachedResponse]

get_response(key, default=None)#

Retrieve a response from the cache, if it exists

Parameters:
  • key (str) – Cache key for the response

  • default – Value to return if key is not in the cache

Return type:

Optional[CachedResponse]

recreate_keys()[source]#

Recreate cache keys for all previously cached responses

remove_expired_responses(expire_after=None)#
Parameters:

expire_after (Union[None, int, float, str, datetime, timedelta]) –

reset_expiration(expire_after=None)#

Set a new expiration value to set on existing cache items

Parameters:

expire_after (Union[None, int, float, str, datetime, timedelta]) – New expiration value, relative to the current time

save_response(response, cache_key=None, expires=None)#

Save a response to the cache

Parameters:
  • cache_key (Optional[str]) – Cache key for this response; will otherwise be generated based on request

  • response (Response) – Response to save

  • expires (Optional[datetime]) – Absolute expiration time for this response

sorted(key='expires', reversed=False, limit=None, expired=True)[source]#

Get cached responses, with sorting and other query options.

Parameters:
  • key (str) – Key to sort by; either ‘expires’, ‘size’, or ‘key’

  • reversed (bool) – Sort in descending order

  • limit (Optional[int]) – Maximum number of responses to return

  • expired (bool) – Set to False to exclude expired responses

Return type:

Iterator[CachedResponse]

update(other)#

Update this cache with the contents of another cache

Parameters:

other (BaseCache) –

urls(**kwargs)#

Get all unique cached URLs. Optionally takes keyword arguments for filter().

Return type:

List[str]

class requests_cache.backends.sqlite.SQLiteDict(db_path, table_name='http_cache', busy_timeout=None, fast_save=False, retries=3, serializer=<requests_cache.serializers.pipeline.SerializerPipeline object>, use_cache_dir=False, use_memory=False, use_temp=False, wal=False, **kwargs)[source]#

Bases: BaseStorage

A dictionary-like interface for SQLite

Parameters:
bulk_commit()[source]#

Insert a large number of records within a single transaction

Example

>>> d1 = SQLiteDict('test')
>>> with d1.bulk_commit():
...     for i in range(1000):
...         d1[i] = i * 2
bulk_delete(keys=None, values=None)[source]#

Delete multiple items from the cache, without raising errors for any missing items. Supports deleting by either key or by value.

clear() None.  Remove all items from D.[source]#
close()[source]#

Close any active connections

connection(commit=False)[source]#

Get a thread-local database connection

Return type:

Iterator[Connection]

count(expired=True)[source]#

Count number of responses, optionally excluding expired

Parameters:

expired (bool) –

Return type:

int

deserialize(key, value)#

Deserialize a value, if a serializer is available.

If deserialization fails (usually due to a value saved in an older requests-cache version), None will be returned.

Parameters:

value (TypeVar(VT)) –

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
init_db()[source]#

Initialize the database, if it hasn’t already been

items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
pop(k[, d]) v, remove specified key and return the corresponding value.#

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair#

as a 2-tuple; but raise KeyError if D is empty.

serialize(value)[source]#

Serialize a value, if a serializer is available

Parameters:

value (TypeVar(VT)) –

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D#
size()[source]#

Return the size of the database, in bytes. For an in-memory database, this will be an estimate based on page size.

Return type:

int

sorted(key='expires', reversed=False, limit=None, expired=True)[source]#

Get cache values in sorted order; see SQLiteCache.sorted() for usage details

Parameters:
Return type:

Iterator[CachedResponse]

update([E, ]**F) None.  Update D from mapping/iterable E and F.#

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

vacuum()[source]#
values() an object providing a view on D's values#
requests_cache.backends.sqlite.get_cache_path(db_path, use_cache_dir=False, use_temp=False)[source]#

Get a resolved cache path

Parameters:
Return type:

Path

requests_cache.backends.sqlite.sqlite_template(timeout=5.0, detect_types=0, isolation_level=None, check_same_thread=True, factory=None, cached_statements=100, uri=False)[source]#

Template function to get an accurate signature for the builtin sqlite3.connect()

Parameters: