aiohttp_client_cache.backends.sqlite module

class aiohttp_client_cache.backends.sqlite.SQLiteBackend(cache_name='aiohttp-cache', **kwargs)[source]

Bases: aiohttp_client_cache.backends.base.CacheBackend

An async SQLite cache backend. Reading is fast, saving is a bit slower. It can store a large amount of data with low memory usage. The path to the database file will be <cache_name>.sqlite, or just <cache_name> if a a different file extension is specified.

Parameters

cache_name (str) – Database filename

See CacheBackend for additional args.

class aiohttp_client_cache.backends.sqlite.SQLiteCache(filename, table_name)[source]

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) – filename for database (without extension)

  • table_name (str) – table name

_abc_impl = <_abc_data object>
async _init_db(db)[source]

Create table if this is the first connection opened, and set fast save if possible

Parameters

db (aiosqlite.core.Connection) –

bulk_commit()[source]

Context manager used to speedup insertion of big number of records

Example

>>> cache = SQLiteCache('test')
>>> async with cache.bulk_commit():
...     for i in range(1000):
...         await cache.write(f'key_{i}', str(i * 2))
async clear()[source]

Delete all items from the cache

async contains(key)[source]

Check if a key is stored in the cache

Return type

bool

Parameters

key (str) –

async delete(key)[source]

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

Parameters

key (str) –

get_connection(autocommit=False)[source]
Return type

AsyncIterator[Connection]

Parameters

autocommit (bool) –

async keys()[source]

Get all keys stored in the cache

Return type

Iterable[str]

async read(key)[source]

Read a single item from the cache. Returns None if the item is missing.

Return type

Union[CachedResponse, None, bytes, str]

Parameters

key (str) –

async size()[source]

Get the number of items in the cache

Return type

int

async values()[source]

Get all values stored in the cache

Return type

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

async write(key, item)[source]

Write an item to the cache

Parameters
class aiohttp_client_cache.backends.sqlite.SQLitePickleCache(filename, table_name)[source]

Bases: aiohttp_client_cache.backends.sqlite.SQLiteCache

Same as SqliteCache, but pickles values before saving

_abc_impl = <_abc_data object>
async read(key)[source]

Read a single item from the cache. Returns None if the item is missing.

Return type

Union[CachedResponse, None, bytes, str]

Parameters

key (str) –

async values()[source]

Get all values stored in the cache

Return type

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

async write(key, item)[source]

Write an item to the cache