Skip to content

Commit

Permalink
Merge pull request #65 from calendar42/master
Browse files Browse the repository at this point in the history
Only update or get the ratelimit cache when it was not inserted
  • Loading branch information
jsocol committed May 11, 2015
2 parents 7c226de + 98901ed commit 5e1aff5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ratelimit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ def is_ratelimited(request, group=None, fn=None, key=None, rate=None,
'Could not understand ratelimit key: %s' % key)

cache_key = _make_cache_key(group, rate, value, method)
cache.add(cache_key, 0)
if increment:
count = cache.incr(cache_key)
initial_value = 1 if increment else 0
added = cache.add(cache_key, initial_value)
if added:
count = initial_value
else:
count = cache.get(cache_key)
if increment:
count = cache.incr(cache_key)
else:
count = cache.get(cache_key)
limited = count > limit
if increment:
request.limited = old_limited or limited
Expand Down

0 comments on commit 5e1aff5

Please sign in to comment.