Commit 97b52156 by Tom Christie

Added `.cache` attribute on throttles.

Closes #1066.  More localised than a new settings key, and more
flexible in that different throttles can use different behavior.
Thanks to @chicheng for the report! :)
parent d7224afe
...@@ -70,6 +70,13 @@ Or, if you're using the `@api_view` decorator with function based views. ...@@ -70,6 +70,13 @@ Or, if you're using the `@api_view` decorator with function based views.
The throttle classes provided by REST framework use Django's cache backend. You should make sure that you've set appropriate [cache settings][cache-setting]. The default value of `LocMemCache` backend should be okay for simple setups. See Django's [cache documentation][cache-docs] for more details. The throttle classes provided by REST framework use Django's cache backend. You should make sure that you've set appropriate [cache settings][cache-setting]. The default value of `LocMemCache` backend should be okay for simple setups. See Django's [cache documentation][cache-docs] for more details.
If you need to use a cache other than `'default'`, you can do so by creating a custom throttle class and setting the `cache` attribute. For example:
class CustomAnonRateThrottle(AnonRateThrottle):
cache = get_cache('alternate')
You'll need to rememeber to also set your custom throttle class in the `'DEFAULT_THROTTLE_CLASSES'` settings key, or using the `throttle_classes` view attribute.
--- ---
# API Reference # API Reference
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Provides various throttling policies. Provides various throttling policies.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
from django.core.cache import cache from django.core.cache import cache as default_cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
import time import time
...@@ -39,6 +39,7 @@ class SimpleRateThrottle(BaseThrottle): ...@@ -39,6 +39,7 @@ class SimpleRateThrottle(BaseThrottle):
Previous request information used for throttling is stored in the cache. Previous request information used for throttling is stored in the cache.
""" """
cache = default_cache
timer = time.time timer = time.time
cache_format = 'throtte_%(scope)s_%(ident)s' cache_format = 'throtte_%(scope)s_%(ident)s'
scope = None scope = None
...@@ -99,7 +100,7 @@ class SimpleRateThrottle(BaseThrottle): ...@@ -99,7 +100,7 @@ class SimpleRateThrottle(BaseThrottle):
if self.key is None: if self.key is None:
return True return True
self.history = cache.get(self.key, []) self.history = self.cache.get(self.key, [])
self.now = self.timer() self.now = self.timer()
# Drop any requests from the history which have now passed the # Drop any requests from the history which have now passed the
...@@ -116,7 +117,7 @@ class SimpleRateThrottle(BaseThrottle): ...@@ -116,7 +117,7 @@ class SimpleRateThrottle(BaseThrottle):
into the cache. into the cache.
""" """
self.history.insert(0, self.now) self.history.insert(0, self.now)
cache.set(self.key, self.history, self.duration) self.cache.set(self.key, self.history, self.duration)
return True return True
def throttle_failure(self): def throttle_failure(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment