API#

class kvsqlite.Client(database: str, table_name: str = 'kvsqlite', autocommit: bool = True, journal_mode: str = 'WAL', synchronous: str = 'NORMAL', default_encoder=<class 'kvsqlite.encoders.PickleEncoder'>, workers: int = 2, loop: ~typing.Optional[~asyncio.events.AbstractEventLoop] = None)#

Kvsqlite asynchronous client

Parameters:
  • database (str) – Sqlite3 database path.

  • table_name (str, optional) – Table name to use, will be created if not exists. Defaults to kvsqlite.

  • autocommit (bool, optional) – Whether autocommit database changes or not. Defaults to True.

  • journal_mode (str, optional) – See https://www.sqlite.org/pragma.html#pragma_journal_mode. Defaults to WAL.

  • synchronous (str, optional) – See https://www.sqlite.org/pragma.html#pragma_synchronous. Defaults to NORMAL.

  • default_encoder (Callable, optional) – The encoder class which deal with the data sent/received by sqlite3. Defaults to kvsqlite.PickleEncoder.

  • workers (int, optional) – The number of workers which process sqlite queries. Defaults to 2.

  • loop (AbstractEventLoop, optional) – Event loop. Defaults to None.

async get(key: str) Any#

Get the value of key

Parameters:

key (str) – The key to get

async set(key: str, value) bool#

Set the value of key

Parameters:
  • key (str) – The key

  • value (Any) – The value to set for key

Returns:

boolTrue on success

async setex(key: str, ttl: int, value) bool#

Set the value of key with a timeout specified by ttl

Parameters:
  • key (str) – The key

  • ttl (int) – The number of seconds for key timeout (a.k.a key lifetime)

  • value (Any) – The value to set for key

Warning

Timeouted keys aren’t deleted by default, you must call cleanex() from time to time

Returns:

boolTrue on success

async delete(key: str) bool#

Delete key from database

Parameters:

key (str) – The key to delete

async commit() bool#

Commit the current changes

Returns:

boolTrue on success

async exists(key: str) bool#

Check if key already exists in database

Parameters:

key (str) – The key to search for

Returns:

boolTrue if found, otherwise False

async ttl(key: str) float#

Returns the remaining time to live of a key that has a timeout

Parameters:

key (str) – The key

Returns:

float – The remaining time, otherwise 0

async expire(key: str, ttl: int) bool#

Set a timeout on key

Parameters:
  • key (str) – The key

  • ttl (int) – The number of seconds for key timeout (a.k.a key lifetime)

Returns:

boolTrue on success

async rename(key: str, new_key: str) bool#

Rename key with new_key

Parameters:
  • key (str) – The key to rename

  • new_key (str) – The key to rename with

Returns:

boolTrue if renamed, otherwise False

async keys(like: str = '%') Optional[List[Tuple[str]]]#

Return list of keys in database with the given pattern

Parameters:

like (str, optional) – SQLite LIKE operator. Defaults to % (all keys)

Returns:

list – A list of Tuple contains keys

None:

If there is no keys to return

async cleanex() int#

Removes all expired keys from database. This reduces disk usage

Returns:

int – Number of deleted keys

async flush() bool#

Flush and remove everything from the current database

Returns:

boolTrue on success

async close(optimize_database: bool = True) bool#

Close database connection

Parameters:

optimize_database (bool, optional) – Whether optimize database before closing or not. Defaults to True

Returns:

boolTrue on success

class kvsqlite.PickleEncoder#

Encoder which uses pickle to serialize/deserialize the object

encode(obj)#
decode(obj)#
class kvsqlite.StringEncoder#

This encoder can be used instead of PickleEncoder. This encoder accpets str only

encode(text)#
decode(text)#