I read django.core.cache's source code ,django handle cache on middleware , 
so I create a new middleware to deal this.

class DispatchMiddleware(MiddlewareMixin):
    def process_response(self, request, response):
        if hasattr(response, 'cache_key'):
            key = response.cache_key
            timeout=response.cache_time
            del response.cache_key
            del response.cache_time
            cache.set(key,response,timeout)
        return response


and on dispatch method 

def dispatch(self, request, *args, **kwargs):
    key = gen_cache_key(request)
    res = cache.get(key)
    if res:
        return res
    res = super().dispatch(request, *args, **kwargs)
    res.cache_key = key
    res.cache_time = 60 * 3
    return res


add cache_key and cache_time attribute to response object.


-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to