Skip to main content

Looks like a dict and acts like a dict but is persistent via an LMDB db

Project description

PersistDict

Just a DIY version sqldict: looks like a dict and acts like a dict but is persistent via an LMDB database. Makes heavy use of lmdb-dict behind the scenes.

Why?

I ran into issue with langchain's caches when developping wdoc (my RAG lib, optimized for my use) and after months of waiting I decided to fix it myself. And instead of trusting sqldict's implementation with langchain's concurrency I made my own. This makes it very easy to add persistent cache to anything. Also it was easy to do thanks to my BrownieCutter. I initially made an implementation that used sqlite (with support for encryption, compression and handled concurrency via a singleton) but then I stumbled upon lmdb-dict which is very probably way better as it's done by pros. It's based on LMDB which is a more suitable for what I was after when doing PersistDict than sqlite3. If you want to use the sqlite version take a look at version before 2.0.0.

Features:

  • threadsafe: if several threads try to access the same db it won't be a problem. Even if multiple other threads use also another db. And if several python scripts run at the same time and try to access the same db, LMDB should make them wait appropriately.
  • atime and ctime: each entry includes a creation time and a last access time.
  • expiration: won't grow too large because old keys are automatically removed after a given amount of days.
  • cached: Uses a LRUCache128 from cachetools.
  • customizable serializer for keys and values: This can enable encryption, compression etc... By default, keys are compressed as lmdb has a 511 default key length.
  • only one dependency needed Only lmdb-dict-full is needed. If you have beartype installed it will be used, same with loguru.

Usage:

  • Download from pypi with pip install PersistDict
  • Or from git:
    • git clone https://github.com/thiswillbeyourgithub/PersistDict
    • cd PersistDict
    • pip install -e .
    • To run tests: cd PersistDict ; python -m pytest test.py
from PersistDict import PersistDict

# create the object
d = PersistDict(
    database_path=a_path,
    # verbose=True,
    # expiration_days=30,
)
# then treat it like a dict:
d["a"] = 1

# You can even create it via __call__, like a dict:
# d = d(a=1, b="b", c=str)  # this actually calls __call__ but is only
# allowed once per PersistDict, just like a regular dict

# it's a child from dict
assert isinstance(d, dict)

# prints like a dict
print(d)
# {'a': 1, 'b': 'b', 'c': str}

# Supports the same methods
assert sorted(list(d.keys())) == ["a", "b", "c"], d
assert "b" in d
del d["b"]
assert list(d.keys()) == ["a", "c"], d
assert len(d) == 2, d
assert d.__repr__() == {"a": 1, "c": str}.__repr__()
assert d.__str__() == {"a": 1, "c": str}.__str__()

# supports all the same types as value as pickle (or more if you change
# the serializer)
d["d"] = None

# If you create another object pointing at the same db, they will share the
# same cache and won't corrupt the db:
d2 = PersistDict(
database_path=dbp,
verbose=True,
)
list(d.keys()) == list(d2.keys()), d2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

persistdict-0.2.1.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

PersistDict-0.2.1-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file persistdict-0.2.1.tar.gz.

File metadata

  • Download URL: persistdict-0.2.1.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.20

File hashes

Hashes for persistdict-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c4be7e1b72d7860016e6f8994a0cf832279530e3322137317e537b5f8aeb691d
MD5 ac08976eb3300fc5d05068dc712dd9b5
BLAKE2b-256 dfa4a8378cf70c1ddd3c3abfb739e52dfe771af19ff3f22efe76493f388ae014

See more details on using hashes here.

File details

Details for the file PersistDict-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: PersistDict-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.20

File hashes

Hashes for PersistDict-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89ac32537321748980d40e64872f5d3d30c99e3e6853ad106b5ef6787ac74b39
MD5 fe893e8925f93bb31b36e030c6dedc6e
BLAKE2b-256 2be4ca9c6ff87a64e3414185b4ce9db2c12d7d2f16f7faa5049ac83cfbc9a6ce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page