Skip to main content

A simple wrapper for key-value stores supporting Python dict, Redis, Valkey, Memcached, and SQLite and other backend clients.

Project description

kv-store

CI PyPI version Python 3.10+ License: MIT

A unified async interface for key-value stores in Python. Write your caching logic once, swap backends without changing code.

Installation

pip install kv-store-py

Or with uv:

uv add kv-store-py

For Redis support:

pip install kv-store-py redis

Quick Start

import asyncio
from kv_store import RedisStore

async def main():
    # Simple: create from URL with automatic resource management
    async with await RedisStore.from_url("redis://localhost:6379/0") as store:
        await store.set("user:1", {"name": "Alice", "email": "alice@example.com"}, ttl=3600)
        user = await store.get("user:1")
        print(user)  # {'name': 'Alice', 'email': 'alice@example.com'}

asyncio.run(main())

Alternative: Inject Your Own Client

from redis.asyncio import Redis
from kv_store import RedisStore

async def main():
    # Advanced: manage the Redis client yourself
    client = Redis(host="localhost", port=6379)
    store = RedisStore(client)

    await store.set("key", "value", ttl=3600)

    # You manage the client lifecycle
    await client.aclose()

API Reference

All store implementations share the same async interface:

Single Key Operations

# Get a value (returns default if key doesn't exist or is expired)
value = await store.get(key: str, default: Any = None) -> Any

# Set a value with TTL in seconds
success = await store.set(key: str, value: Any, ttl: int) -> bool

# Delete a key
existed = await store.delete(key: str) -> bool

Batch Operations

# Get multiple keys at once
results = await store.get_many(
    keys: list[str],
    default: Any = None  # Single value or list of defaults per key
) -> dict[str, Any]

# Set multiple keys atomically
results = await store.set_many(items: dict[str, Any], ttl: int) -> dict[str, bool]

# Delete multiple keys
count = await store.delete_many(keys: list[str]) -> int

Distributed Locking (Redis only)

# Acquire a lock for mutual exclusion
async with store.lock("resource:id", timeout=30, blocking_timeout=10):
    # Critical section - only one process can execute this
    await perform_exclusive_operation()

Supported Backends

Backend Status Class
Redis Available RedisStore
In-Memory Available InMemoryStore
Valkey Planned -
SQLite Planned -
PostgreSQL Planned -
MySQL Planned -

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE for details.

Download files

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

Source Distribution

kv_store_py-0.0.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

kv_store_py-0.0.4-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file kv_store_py-0.0.4.tar.gz.

File metadata

  • Download URL: kv_store_py-0.0.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kv_store_py-0.0.4.tar.gz
Algorithm Hash digest
SHA256 61f1433b0f0e12b6ec4ae9079c3a578bad106a86eded92e1656e2d3cd0a6cacd
MD5 b94c9ef77374ee079febde7d91a6d654
BLAKE2b-256 97e09220416ab13133169826516d2ca56a843965714975c25c18654b9e59f84d

See more details on using hashes here.

File details

Details for the file kv_store_py-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: kv_store_py-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kv_store_py-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 45b0201015a4930bb8f431dbffb312abbb71312964a9c88b5a66ab5a8841ed2a
MD5 6547875f69d48e95fdd40994142a83b7
BLAKE2b-256 d9eec463deaf1471a887bf04712eaab079b82024a1ff20c482fbe590ea7c1844

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