Idempotency-key decorator for exactly-once handlers, backed by Redis or in-memory.
Project description
kasper-idempotent
Decorate a handler with an idempotency key. Duplicate calls (retries, at-least-once queues) return the stored result instead of running again.
pip install kasper-idempotent # in-memory store
pip install "kasper-idempotent[redis]" # + Redis store (multi-process)
from kasper_idempotent import idempotent, RedisStore
store = RedisStore("redis://localhost:6379")
@idempotent(store=store, key=lambda evt: evt["id"], ttl=3600)
def handle(evt):
charge_card(evt) # runs once per evt["id"], even on retries
return {"ok": True}
How it works
On call it atomically claims the key (SET key ... NX):
- claim wins -> run the handler, store the result, return it.
- already done -> return the cached result (no re-run).
- in progress -> raise
InProgress(a concurrent duplicate is still running).
The atomic claim is what prevents two simultaneous duplicates from both executing. Sync and async handlers are both supported.
MIT licensed.
Project details
Release history Release notifications | RSS feed
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 kasper_idempotent-0.1.0.tar.gz.
File metadata
- Download URL: kasper_idempotent-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54131041853d998ed0789b17393f61ce87345e186c801bf806ddcb24d257f55b
|
|
| MD5 |
c504e19aad9c62106ccf5063ed958b7d
|
|
| BLAKE2b-256 |
ca2507ff2205ff573d2ce423fb598836df87330bec5034abb2d804feac8ef8a1
|
File details
Details for the file kasper_idempotent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kasper_idempotent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03572401d7f440cda5bea82024ee80b4810ff84552107767e345fee4bb684e21
|
|
| MD5 |
3720009aef2ff3f584a06180d200d924
|
|
| BLAKE2b-256 |
762a3c13086b661fbf8d5f50f3b81ca1061d38612a78251a00e2ed5babdd2830
|