Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache Django Rest Framework API calls?

I'm using Memcached as backend to my django app. This code works fine in normal django query:

def get_myobj():         cache_key = 'mykey'         result = cache.get(cache_key, None)         if not result:             result = Product.objects.all().filter(draft=False)             cache.set(cache_key, result)         return result 

But it doesn't work when used with django-rest-framework api calls:

class ProductListAPIView(generics.ListAPIView):     def get_queryset(self):         product_list = Product.objects.all()         return product_list     serializer_class = ProductSerializer 

I'm about to try DRF-extensions which provide caching functionality:

https://github.com/chibisov/drf-extensions

but the build status on github is currently saying "build failing".

My app is very read-heavy on api calls. Is there a way to cache these calls?

Thank you.

like image 355
Kitti Wateesatogkij Avatar asked Jul 12 '16 04:07

Kitti Wateesatogkij


People also ask

CAN REST API be cached?

Caching in REST APIs POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

Does Django have caching?

Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.


1 Answers

Ok, so, in order to use caching for your queryset:

class ProductListAPIView(generics.ListAPIView):     def get_queryset(self):         return get_myobj()     serializer_class = ProductSerializer 

You'd probably want to set a timeout on the cache set though (like 60 seconds):

cache.set(cache_key, result, 60) 

If you want to cache the whole view:

from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page  class ProductListAPIView(generics.ListAPIView):     serializer_class = ProductSerializer      @method_decorator(cache_page(60))     def dispatch(self, *args, **kwargs):         return super(ProductListAPIView, self).dispatch(*args, **kwargs) 
like image 148
Linovia Avatar answered Sep 25 '22 10:09

Linovia