A simple wrapper for key-value stores supporting Python dict, Redis, Valkey, Memcached, and SQLite and other backend clients.
Project description
kv-store
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kv_store_py-0.0.5.tar.gz.
File metadata
- Download URL: kv_store_py-0.0.5.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a7e9909295cb4983273c2bc753a04a71cb41a5dd6466b2c73674aad8af0c0ba
|
|
| MD5 |
675707047a556cd5d16703dd3ef568d1
|
|
| BLAKE2b-256 |
77764e4170f960310b4605160acbbb54aa4c6fa7f6744a0147d9c76a39c59ac9
|
File details
Details for the file kv_store_py-0.0.5-py3-none-any.whl.
File metadata
- Download URL: kv_store_py-0.0.5-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcd3700a0f7e0ccab96a6ed02f1c7b3132f1152609309e31483a884859a93e9e
|
|
| MD5 |
11691cabdecbf74c2a5ed6b5bf12c965
|
|
| BLAKE2b-256 |
6950e80606a293fe1def093b76ca57b2094a1d1677951ece4f2373e23abdc382
|