Skip to main content

A simple wrapper for key-value stores supporting Python dict, Redis, Valkey, PostgreSQL, 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

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.

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.6.tar.gz (10.4 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.6-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kv_store_py-0.0.6.tar.gz
  • Upload date:
  • Size: 10.4 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.6.tar.gz
Algorithm Hash digest
SHA256 2b6e5dc06cb8f1f3ed317279b089c2893950dee6b4d2c08a6a89c40e0dbf1dfc
MD5 4d4c2f58af2784da24a262d8eb0fb199
BLAKE2b-256 99fbc242d728dff05d7c10dbbc20dc01a9f5bf683705af4aa177cc0f2556ee98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kv_store_py-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.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

Hashes for kv_store_py-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e4aba39840586764e4d9cfb7ddb7dafafc9fcb9814b2a0d0d2352663b91a4c91
MD5 93179a1e16eeb00c6312499c487e5732
BLAKE2b-256 b180223119f2c4490aeedee9b184686a0a5f87619a81d30a032cf9d9d2669946

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