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 tokvsqlite
.autocommit (
bool
, optional) – Whether autocommit database changes or not. Defaults toTrue
.journal_mode (
str
, optional) – See https://www.sqlite.org/pragma.html#pragma_journal_mode. Defaults toWAL
.synchronous (
str
, optional) – See https://www.sqlite.org/pragma.html#pragma_synchronous. Defaults toNORMAL
.default_encoder (
Callable
, optional) – The encoder class which deal with the data sent/received by sqlite3. Defaults tokvsqlite.PickleEncoder
.workers (
int
, optional) – The number of workers which process sqlite queries. Defaults to2
.loop (
AbstractEventLoop
, optional) – Event loop. Defaults toNone
.
- async set(key: str, value) bool #
Set the value of
key
- Parameters:
key (
str
) – The keyvalue (
Any
) – The value to set forkey
- Returns:
bool
–True
on success
- async setex(key: str, ttl: int, value) bool #
Set the value of
key
with a timeout specified byttl
- Parameters:
key (
str
) – The keyttl (
int
) – The number of seconds forkey
timeout (a.k.akey
lifetime)value (
Any
) – The value to set forkey
Warning
Timeouted keys aren’t deleted by default, you must call
cleanex()
from time to time- Returns:
bool
–True
on success
- async exists(key: str) bool #
Check if
key
already exists in database- Parameters:
key (
str
) – The key to search for- Returns:
bool
–True
if found, otherwiseFalse
- 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, otherwise0
- async expire(key: str, ttl: int) bool #
Set a timeout on
key
- Parameters:
key (
str
) – The keyttl (
int
) – The number of seconds forkey
timeout (a.k.akey
lifetime)
- Returns:
bool
–True
on success
- async rename(key: str, new_key: str) bool #
Rename
key
withnew_key
- Parameters:
key (
str
) – The key to renamenew_key (
str
) – The key to rename with
- Returns:
bool
–True
if renamed, otherwiseFalse
- 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 ofTuple
contains keysNone
: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
- 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 accpetsstr
only- encode(text)#
- decode(text)#