1

I have a Django project and API view implemented with the Rest framework. I'm caching it using the @cache_page decorator but I need to implement a cache invalidation and I'm not seeing how to do that - do I need a custom decorator?

The problem: The view checks the access of the API KEY and it caches it from the previous access check but, if the user changes the API KEY before the cache expires, the view will return an OK status of the key that no longer exists.

1 Answer 1

1

Yes, you'll need a cache decorator that takes the authentication/user context into account. cache_page() only works for GET requests, and keys based on the URL alone.

Better yet, though,

  1. Don't use a cache until you're sure you need one
  2. If you do need it (think about why; cache invalidation is one of the two hard things), use a more granular cache within your view, not cache_page().
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, i'm using it with a key based url on a GET req, i really need it because im getting slow responses from that view. Do you recommendo implement my own cache_page decorator?
No. Use a granular cache within that view, then; cache the data that's slow to retrieve.
Got it! Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.