Skip to content

Commit

Permalink
expire_after is now a method, not a property - which means it always …
Browse files Browse the repository at this point in the history
…takes the current value for self.minutes in to account. Thanks Matthew Somerville.
  • Loading branch information
Simon Willison committed Sep 24, 2009
1 parent 7f52ce1 commit 591db0e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ratelimitcache.py
Expand Up @@ -10,7 +10,6 @@ class ratelimit(object):
requests = 20 # Number of allowed requests in that time period

prefix = 'rl-' # Prefix for memcache key
expire_after = (minutes + 1) * 60

def __init__(self, **options):
for key, value in options.items():
Expand Down Expand Up @@ -44,10 +43,10 @@ def cache_incr(self, key):
# memcache is only backend that can increment atomically
try:
# add first, to ensure the key exists
cache._cache.add(key, '0', time=self.expire_after)
cache._cache.add(key, '0', time=self.expire_after())
cache._cache.incr(key)
except AttributeError:
cache.set(key, cache.get(key, 0) + 1, self.expire_after)
cache.set(key, cache.get(key, 0) + 1, self.expire_after())

def should_ratelimit(self, request):
return True
Expand Down Expand Up @@ -80,7 +79,10 @@ def key_extra(self, request):
def disallowed(self, request):
"Over-ride this method if you want to log incidents"
return HttpResponseForbidden('Rate limit exceeded')


def expire_after(self):
"Used for setting the memcached cache expiry"
return (self.minutes + 1) * 60

class ratelimit_post(ratelimit):
"Rate limit POSTs - can be used to protect a login form"
Expand All @@ -97,5 +99,3 @@ def key_extra(self, request):
extra += '-' + value
return extra



0 comments on commit 591db0e

Please sign in to comment.