Skip to content

Instantly share code, notes, and snippets.

@cshoe
Created January 25, 2013 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshoe/4637419 to your computer and use it in GitHub Desktop.
Save cshoe/4637419 to your computer and use it in GitHub Desktop.
Throttling with cache
def inc_and_get_request_count(token):
"""Increment the request count for `token` and return the new count.
"""
cache_key = COUNT_CACH_TEMPLATE.format(token)
try:
count = cache.incr(cache_key)
except ValueError:
# key doesn't exist in cache. insert it, start it at 1
count = 1
# cache for an hour. this will enforce our count/hour
cache.set(cache_key, count, 3600)
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment