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
    • 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.131.tar.gz (13.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.131-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asyncblobdict-0.1.131.tar.gz
  • Upload date:
  • Size: 13.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.131.tar.gz
Algorithm Hash digest
SHA256 50df037467774903b962508c9483c6078e86ca003ed1ed33f37a2063c7c668d3
MD5 39ee5054883af42333c979e0b2d81a2a
BLAKE2b-256 a0d42f65dd3a35cd2e0a13f542825a130dd05974ce047dc8e08a66823bc986cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for asyncblobdict-0.1.131-py3-none-any.whl
Algorithm Hash digest
SHA256 4a0090cd76064c6db9923c6f92da5134286b004a986d4e972a4cefb867aeee3c
MD5 b0921c2c2d4acd67f5d6d0a0ec887da3
BLAKE2b-256 5b1db27a64affca03f1e375d815388dddbb80a9ff4161fbdacf2a374af074d9d

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