Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kasper_idempotent-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kasper_idempotent-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

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

Hashes for kasper_idempotent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 54131041853d998ed0789b17393f61ce87345e186c801bf806ddcb24d257f55b
MD5 c504e19aad9c62106ccf5063ed958b7d
BLAKE2b-256 ca2507ff2205ff573d2ce423fb598836df87330bec5034abb2d804feac8ef8a1

See more details on using hashes here.

File details

Details for the file kasper_idempotent-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kasper_idempotent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03572401d7f440cda5bea82024ee80b4810ff84552107767e345fee4bb684e21
MD5 3720009aef2ff3f584a06180d200d924
BLAKE2b-256 762a3c13086b661fbf8d5f50f3b81ca1061d38612a78251a00e2ed5babdd2830

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