Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django REST Framework Security and Customization Enhancing Your Calm with Throttling

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Caching and throttling.

Hi, Kenneth Love !

I tried to set up caching (MemcachedCache and DummyCache).

This is how I set up cache:

  1. Installed python-memcached.
  2. Set CACHED in settings.py:

    CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:8000',
    }
    }
    
  3. Created cache table (python manage.py createcachetable)

Then I tried it with postman:

It looks like throttling doesn't work with cache enabled.

  1. I send a token-authorized get request to "http://127.0.0.1:8000/api/v2/courses/".

  2. I get back correct response, but throttling doesn't stop me.

  3. I get this in console, after each request:

code 400, message Bad HTTP/0.9 request type ('get')
"get :1:throttle_user_1" 400 -
"GET /api/v2/courses/ HTTP/1.1" 200 490

The same result with DummyCache.

What am I doing wrong?

Sincerely, Alexey.

Update: Is something wrong with LOCATION? (changet it to '127.0.0.1:11211') like in docs, console output changed to

GET /api/v2/courses/ HTTP/1.1  200 490

on same request, but throttling still doesn't work.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, I'd think your cache location should be the memcached address.

Can you show whatever code you have for the throttle? And how are you testing that it isn't working? Just getting requests through when they should be blocked?

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Thank you for response, Kenneth Love !

Yeah, I'd think your cache location should be the memcached address.

Didn't find how to set up MemCached for local development. LOCATION etc..

Can you show whatever code you have for the throttle?

The same as in example from your video. I just added CACHES to settings.py

And how are you testing that it isn't working? Just getting requests through when they should be blocked?

I just send authorized requests from Postman until "Request was throttled..." response.

Also: In the docs there is another option actually made for local development, called DummyCaching:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

When I add this chunk of code to settings.py throttling also stops working.