Created
January 25, 2013 20:09
-
-
Save cshoe/4637419 to your computer and use it in GitHub Desktop.
Throttling with cache
This file contains 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
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