Created
April 23, 2025 23:34
-
-
Save Graeme22/9e64ca1cc69bf6607e92217add1e84a5 to your computer and use it in GitHub Desktop.
Idempotency keys with redis-py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
from redis import Redis | |
def pydempotent(redis: Redis, ttl: timedelta | int | None, key: str): | |
""" | |
Shields a block of code from repeated execution. | |
""" | |
return redis.set(f"pydempotent:{key}", 1, nx=True, ex=ttl) | |
redis = Redis.from_url("redis://localhost:6379") | |
if pydempotent(redis, 3600, "test_key"): | |
print("doing stuff!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment