Skip to content

Instantly share code, notes, and snippets.

@Graeme22
Created April 23, 2025 23:34
Show Gist options
  • Save Graeme22/9e64ca1cc69bf6607e92217add1e84a5 to your computer and use it in GitHub Desktop.
Save Graeme22/9e64ca1cc69bf6607e92217add1e84a5 to your computer and use it in GitHub Desktop.
Idempotency keys with redis-py
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