Async Key-Value Store - Pluggable Interface

0.4.4 · active · verified Sat Mar 28

py-key-value-aio is an asynchronous Python library offering a pluggable interface for various Key-Value (KV) stores. It provides a backend-agnostic abstraction layer, allowing framework authors and applications to integrate KV storage without committing to a specific implementation like Redis, DynamoDB, or an in-memory solution. The library, currently at version 0.4.4, focuses exclusively on async/await patterns and requires Python >=3.10. It is actively maintained as part of a larger monorepo.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `put`, `get`, and `delete` operations using the `AsyncKeyValue` protocol and an in-memory store. It highlights the asynchronous nature of the library and the common pattern of accepting the `AsyncKeyValue` protocol in your application logic, allowing the underlying store implementation to be swapped easily.

import asyncio
from key_value.aio.protocols.key_value import AsyncKeyValue
from key_value.aio.stores.memory import MemoryStore

async def example_usage(key_value_store: AsyncKeyValue):
    print(f"Putting key 'user:123' with value {{'name': 'Alice'}}")
    await key_value_store.put(key="user:123", value={"name": "Alice"}, collection="users", ttl=3600)
    
    print("Retrieving 'user:123'...")
    user_data = await key_value_store.get(key="user:123", collection="users")
    print(f"Retrieved: {user_data}")

    print("Deleting 'user:123'...")
    await key_value_store.delete(key="user:123", collection="users")

    print("Attempting to retrieve 'user:123' again...")
    deleted_user_data = await key_value_store.get(key="user:123", collection="users")
    print(f"Retrieved after deletion: {deleted_user_data}") # Should be None

async def main():
    # Example with MemoryStore
    memory_store = MemoryStore()
    await example_usage(memory_store)

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

view raw JSON →