sqlite#

Summary#

SQLiteBackend([cache_name, use_temp, fast_save])

Async cache backend for SQLite (requires aiosqlite)

SQLiteCache(filename[, table_name, ...])

An async interface for caching objects in a SQLite database.

SQLitePickleCache(filename[, table_name, ...])

Same as SqliteCache, but pickles values before saving

Module Contents#

class SQLiteBackend(cache_name='aiohttp-cache', use_temp=False, fast_save=False, **kwargs)#

Bases: aiohttp_client_cache.backends.base.CacheBackend

Async cache backend for SQLite (requires aiosqlite)

The path to the database file will be <cache_name> (or <cache_name>.sqlite if no file extension is specified)

Parameters
use_temp: Store database in a temp directory (e.g., /tmp/http_cache.sqlite).

Note: if cache_name is an absolute path, this option will be ignored.

fast_save: Increas cache write performance, but with the possibility of data loss. See

pragma: synchronous for details.

__init__(cache_name='aiohttp-cache', use_temp=False, fast_save=False, **kwargs)#
Parameters
Parameters
class SQLiteCache(filename, table_name='aiohttp-cache', use_temp=False, fast_save=False, **kwargs)#

Bases: aiohttp_client_cache.backends.base.BaseCache

An async interface for caching objects in a SQLite database.

Example

>>> # Store data in two tables under the 'testdb' database
>>> d1 = SQLiteCache('testdb', 'table1')
>>> d2 = SQLiteCache('testdb', 'table2')
Parameters
  • filename (str) – Database filename

  • table_name (str) – Table name

  • use_temp (bool) – Store database in a temp directory (e.g., /tmp/http_cache.sqlite). Note: if cache_name is an absolute path, this option will be ignored.

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

  • fast_save (bool) –

_abc_impl = <_abc_data object>#
async _init_db()#

Initialize the database, if it hasn’t already been

bulk_commit()#

Contextmanager to more efficiently write a large number of records at once

Example

>>> cache = SQLiteCache('test')
>>> async with cache.bulk_commit():
...     for i in range(1000):
...         await cache.write(f'key_{i}', str(i))
async bulk_delete(keys)#

Delete item(s) from the cache. Does not raise an error if the item is missing.

Parameters

keys (set) –

async clear()#

Delete all items from the cache

async close()#

Close any open connections

async contains(key)#

Check if a key is stored in the cache

Parameters

key (str) –

Return type

bool

async delete(key)#

Delete an item from the cache. Does not raise an error if the item is missing.

Parameters

key (str) –

get_connection(commit=False)#
Parameters

commit (bool) –

Return type

AsyncIterator[Connection]

async keys()#

Get all keys stored in the cache

Return type

AsyncIterable[str]

async read(key)#

Read an item from the cache. Returns None if the item is missing.

Parameters

key (str) –

Return type

Union[CachedResponse, bytes, str, None]

async size()#

Get the number of items in the cache

Return type

int

async values()#

Get all values stored in the cache

Return type

AsyncIterable[Union[CachedResponse, bytes, str, None]]

async write(key, item)#

Write an item to the cache

Parameters
class SQLitePickleCache(filename, table_name='aiohttp-cache', use_temp=False, fast_save=False, **kwargs)#

Bases: aiohttp_client_cache.backends.sqlite.SQLiteCache

Same as SqliteCache, but pickles values before saving

Parameters
  • filename (str) –

  • table_name (str) –

  • use_temp (bool) –

  • fast_save (bool) –

  • kwargs (Any) –

_abc_impl = <_abc_data object>#
_connection: Optional[aiosqlite.core.Connection]#
async read(key)#

Read an item from the cache. Returns None if the item is missing.

Parameters

key (str) –

Return type

Union[CachedResponse, bytes, str, None]

async values()#

Get all values stored in the cache

Return type

AsyncIterable[Union[CachedResponse, bytes, str, None]]

async write(key, item)#

Write an item to the cache

_get_cache_filename(filename, use_temp)#

Get resolved path for database file

Parameters
Return type

str