Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,810 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,810 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.