Diego Moraes
Diego Moraes

Reputation: 33

Django REST Cache Invalidation

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.

Upvotes: 1

Views: 781

Answers (1)

AKX
AKX

Reputation: 168834

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().

Upvotes: 1

Related Questions