Django cache REST API Urls issue

I have implemented the solution provided in the link and it works fine when I use it from my browser. However, when I tried to use this url with curl, it does not cache the browser.

Let me explain.

If I hit url like example.org/results?limit=7from my chrome, it takes 8-10 secondsto load, and consecutive hits take timemilliseconds

So I did this URLwith the command curl; but he did not use cached data and created the cache again.

So, I found out that the problem is related to the parameter argin the code below, since it contains the browser headers in the object WSGIRequestthat is used in caching, because it contains headers that I do not need for caching. This causes my curl requests to fail to automatically create a cache from celery task.

@method_decorator(cache_page(60 * 60 * 24))
def dispatch(self, *arg, **kwargs):
    print(arg)
    print(kwargs)
    return super(ProfileLikeHistoryApi, self).dispatch(*arg, **kwargs)  

What I can do is pass only kwargsto create a cache or any other alternative with which I can do a cache for URLs, not headers

Thanks for the help in advance.

+6
source share
1 answer

TL; DR; Delete method handler and cache manually

from django.core.cache import cache
from django.utils.encoding import force_bytes, force_text, iri_to_uri
import hashlib

def dispatch(self, *arg, **kwargs):

    if self.request.method == 'GET' or self.request.method == 'HEAD':
        key = hashlib.md5(force_bytes(iri_to_uri(self.request.build_absolute_uri()))))
        data = cache.get(key)
        if not data:
            data = super(ProfileLikeHistoryApi, self).dispatch(*arg, **kwargs)  
            cache.set(key, data, 60*60*24)
            return data

   return super(ProfileLikeHistoryApi, self).dispatch(*arg, **kwargs)  

cache_page

, , - , . " . , .

-, GET HEAD ( ), .

md5

, , . , , . , django _ generate_cache_key, .

, , . . , 23 59 .

cron, , 6 .

, , memcached, , redis db, .

+3

All Articles