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

Easy and practical key value store.

  • get, set, get_many, set_many methods with handy defaults
  • Implemented as Asyncrounous context manager
  • Supports distributed locking in all implementations

Installation

pip install kv-store-py

Quick Start

from kv_store_py 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:

Core 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

# 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()

PostgreSQL Options

When using PostgresStore.from_url(), you can customize the connection pool and table name:

store = await PostgresStore.from_url(
    "postgresql://localhost/mydb",
    table_name="custom_kv",  # Default: "kv_store"
    min_size=2,              # Minimum pool connections
    max_size=10              # Maximum pool connections
)

PostgreSQL uses table-based locking with auto-expiration tokens (not advisory locks).

Supported Backends

Backend Status Class
In-Memory Available InMemoryStore
Redis Available RedisStore
PostgreSQL Available PostgresStore

Note: PostgreSQL support was tested only on PostgreSQL 16. There is no guarantee it works on other versions.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: kv_store_py-1.0.0.tar.gz
  • Upload date:
  • Size: 11.1 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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a722eabbde6071ea77081d544ebbfa76c395889aaf757107dab0e011f03188a2
MD5 3d294240ad103564a0055dbf6d3cd766
BLAKE2b-256 55d64149575530feda868c78affc4e91a2b5f8e8dc22b363be59d2edf3382aa1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kv_store_py-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01d93e93e70565b78697d7c7757478a8a4358fe476dc602e864143d99f321cce
MD5 637d2854d9f87f156b37b22b1e040570
BLAKE2b-256 3603445225d5ad63e87a8208caa72fc7cc4febeb2d77433941b5fac478303ad8

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