Permalink
Browse files

expire_after is now a method, not a property - which means it always …

…takes the current value for self.minutes in to account. Thanks Matthew Somerville.
  • Loading branch information...
1 parent 7f52ce1 commit 591db0eb3a7270741a2032d3d8ededdec454e4f4 Simon Willison committed Sep 24, 2009
Showing with 6 additions and 6 deletions.
  1. +6 −6 ratelimitcache.py
View
@@ -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():
@@ -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
@@ -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"
@@ -97,5 +99,3 @@ def key_extra(self, request):
extra += '-' + value
return extra
-
-

0 comments on commit 591db0e

Please sign in to comment.