Skip to main content

Minimal async key-value store backed by local or cloud blob storage

Project description

Tests codecov

asyncblobdict

asyncblobdict is a minimal async‑friendly key–value store for JSON and binary data, backed by either:

  • Local filesystem
  • Azure Blob Storage

It lets you store and retrieve Python data by key, with optional caching and optimistic concurrency via ETags. Swap backends without changing your application code.


Features

  • Backends: Local filesystem or Azure Blob Storage
  • Data types:
    • JSON‑serializable (dict, list, set, datetime, etc.)
    • Arbitrary binary (bytes, bytearray)
  • Caching modes:
    • NONE - always read/write directly to backend
    • READ - cache reads only
    • WRITE_THROUGH - cache and immediately write to backend
    • WRITE_BACK - cache and sync later
  • Concurrency modes:
    • NONE - no concurrency control
    • ETAG - optimistic concurrency using ETags

Installation

From PyPI:

pip install asyncblobdict

For development (editable install):

git clone https://github.com/ViLahte/asyncblobdict.git
cd asyncblobdict
pip install -e .

Quick Start

Local backend example

import asyncio
import pickle
from datetime import datetime
from asyncblobdict import AsyncBlobStore, CacheMode, ConcurrencyMode, LocalFileAdapter, BlobNotFoundError

async def main():
    adapter = LocalFileAdapter("./local_blob_storage")
    async with AsyncBlobStore(
        adapter,
        "demo_container",
        cache_mode=CacheMode.WRITE_THROUGH,
        concurrency_mode=ConcurrencyMode.ETAG,
    ) as store:
        # Store JSON
        config = {"learning_rate": 0.01, "layers": [64, 128, 256], "created_at": datetime.utcnow()}
        await store.set_json("demo/config", config)

        # Retrieve JSON
        loaded_config = await store.get_json("demo/config")
        print("Loaded config:", loaded_config)

        # Store binary
        model_bytes = pickle.dumps({"weights": [0.1, 0.2, 0.3], "bias": 0.5})
        await store.set_binary("demo/model.bin", model_bytes)

        # Retrieve binary
        loaded_model = pickle.loads(await store.get_binary("demo/model.bin"))
        print("Loaded model:", loaded_model)

        # List keys
        print("Keys:", await store.list_keys())

        # Delete a key
        await store.delete("demo/config")
        try:
            await store.get_json("demo/config")
        except BlobNotFoundError:
            print("Config deleted successfully.")

if __name__ == "__main__":
    asyncio.run(main())

Azure backend example

Requires environment variables:

AZURE_CONN_STR=your-azure-connection-string
AZURE_CONTAINER=your-container-name
import os
import asyncio
from datetime import datetime
from dotenv import load_dotenv
from asyncblobdict import AsyncBlobStore, CacheMode, ConcurrencyMode, AzureBlobAdapter

load_dotenv()

async def main():
    adapter = AzureBlobAdapter.from_connection_string(os.environ["AZURE_CONN_STR"])
    async with AsyncBlobStore(
        adapter,
        os.environ["AZURE_CONTAINER"],
        cache_mode=CacheMode.WRITE_THROUGH,
        concurrency_mode=ConcurrencyMode.ETAG,
    ) as store:
        await store.set_json("demo/config", {"created_at": datetime.utcnow()})
        print(await store.get_json("demo/config"))

if __name__ == "__main__":
    asyncio.run(main())

Syncing in WRITE_BACK mode

When using CacheMode.WRITE_BACK, changes are cached locally until you call:

await store.sync(etag_behavior="skip")  # options: "skip", "overwrite", "raise"

Testing

We use pytest:

pytest

Azure tests are skipped automatically if environment variables are not set. You can also run only local tests:

pytest -m "not azure"

Or only Azure tests:

pytest -m azure

For code coverage html report:

pytest --cov=src --cov-report=html

License

MIT License

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

asyncblobdict-0.1.11.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncblobdict-0.1.11-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file asyncblobdict-0.1.11.tar.gz.

File metadata

  • Download URL: asyncblobdict-0.1.11.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for asyncblobdict-0.1.11.tar.gz
Algorithm Hash digest
SHA256 82dbc705b7d1addf5a3167e51a5ae01eff536855968126a70493df5012bd0d32
MD5 130f9ebf39fa34aeb4f364c49fcfcab3
BLAKE2b-256 113da47477900496fd5f61b3e7f95e7770077545f87dc5c786a8f6a56917ec32

See more details on using hashes here.

File details

Details for the file asyncblobdict-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: asyncblobdict-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for asyncblobdict-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 d61ad0ba3629cbb4123c98cfa8cf0da5a55dd6fb462b75f58dd696d279f6eebb
MD5 e49c4a3d8af8b45e50e6bf264fbeee54
BLAKE2b-256 91bf11aaa962c152273292bb09912a0052d332ff6b2d5725da97710bba629651

See more details on using hashes here.

Supported by

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