Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Created September 2, 2013 12:08
Show Gist options
  • Save bulv1ne/6412192 to your computer and use it in GitHub Desktop.
Save bulv1ne/6412192 to your computer and use it in GitHub Desktop.
Ratelimit in Python and Redis
import redis
r = redis.Redis()
def ratelimit(key):
EXPIRE = 10
RATE = 10
value = r.incr(str(key))
if value == 1 or r.ttl(str(key)) is None:
r.expire(str(key), EXPIRE)
if value > RATE:
raise Exception('Overflow')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment