A simple wrapper for key-value stores supporting Python dict, Redis, Valkey, PostgreSQL, 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
from kv_store import AbstractStore, RedisStore, PostgresStore
# Use Redis
store: AbstractStore = await RedisStore.from_url("redis://localhost:6379/0")
await store.set("key", "value", ttl=3600)
await store.get("key")
# Swap to Postgres - same interface
store: AbstractStore = await PostgresStore.from_url("postgresql://localhost/mydb")
await store.set_many({"k1": "v1", "k2": "v2"}, ttl=3600)
await store.get_many(["k1", "k2"])
# Distributed locking (supported by both Redis and Postgres)
async with store.lock("my-resource", timeout=30):
# critical section
pass
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
# 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 |
|---|---|---|
| In-Memory | Available | InMemoryStore |
| Redis | Available | RedisStore |
| PostgreSQL | Available | PostgresStore |
| Valkey | Planned | - |
| SQLite | 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.7.tar.gz.
File metadata
- Download URL: kv_store_py-0.0.7.tar.gz
- Upload date:
- Size: 10.8 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 |
4e5d43ac203ec272d12bef1f382eb15abe0868bd64a24db63edfdf7d39be9e50
|
|
| MD5 |
2453d0c141049da388d1d5327fa77488
|
|
| BLAKE2b-256 |
73e548cd333eca0dbb0c23ae926496740cf784d764c1473ccf41832b5b5af670
|
File details
Details for the file kv_store_py-0.0.7-py3-none-any.whl.
File metadata
- Download URL: kv_store_py-0.0.7-py3-none-any.whl
- Upload date:
- Size: 12.1 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 |
e53155295f913a56fd9b0ac8931b246ab88f1682a9b8c9d4129035b57dbff9a2
|
|
| MD5 |
f6b2b492bcb13729e8e6b6cf094a7a59
|
|
| BLAKE2b-256 |
d05b805cf25b5a494601fd1b6865c7e5bb766e13c4894564030c1d1fc6491aa3
|