PersistentKeyValueStore

Documentation for eth_defi.sqlite_cache.PersistentKeyValueStore Python class.

class PersistentKeyValueStore

Bases: dict

A simple key-value cache for sqlite3, honouring Python dictionary interface.

Designed to cache - JSON blobs from integrated API services like TokenSniffer - Token metadata with fetch_erc20_details()

Based on https://stackoverflow.com/questions/47237807/use-sqlite-as-a-keyvalue-store

  • Disk cache can grow over time (supports append)

  • Cache keys must be strings

  • Cache values must be string-encodeable via encode_value() and decode_value() hooks

  • Can be used across threads

Parameters
  • filename – Path to the sqlite database

  • autocommit – Whether to autocommit every time new entry is added to the database

Attributes summary

conn

One connection per thread

Methods summary

__init__(filename[, autocommit])

param filename

Path to the sqlite database

clear()

close()

commit()

copy()

decode_value(value)

Hook to convert SQLite values to Python objects

encode_value(value)

Hook to convert Python objects to cache format

fromkeys([value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

get_file_size()

items()

iteritems()

iterkeys()

itervalues()

keys()

pop(k[,d])

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

purge()

Delete all keys and save.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

update([E, ]**F)

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

values()

__init__(filename, autocommit=True)
Parameters
  • filename (pathlib.Path) – Path to the sqlite database

  • autocommit – Whether to autocommit every time new entry is added to the database

property conn: sqlite3.Connection

One connection per thread

encode_value(value)

Hook to convert Python objects to cache format

Parameters

value (Any) –

Return type

str

decode_value(value)

Hook to convert SQLite values to Python objects

Parameters

value (str) –

Return type

Any

keys() a set-like object providing a view on D's keys
values() an object providing a view on D's values
items() a set-like object providing a view on D's items
get(key, default=None)

Return the value for key if key is in the dictionary, else default.

purge()

Delete all keys and save.

__new__(**kwargs)
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

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

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