0

Within the view itself, I want to be able to set the cache timeout value upon returning a result. The reason for this is if the view returned an error, I want the timeout to be shorter.

Right now, I have the timeout set as static:

url(r'^view/(.+)/', cache_page(24 * 60 * 60)(MyView.as_view()), name='view')

1 Answer 1

1

You can do it manually in your view class. Something like:

class MyView(View):
    def get(self, *args, **kwargs):
        response_data = cache.get('some_key')
        if response_data is None:
            response = super(MyView, self).get(*args, **kwargs)
            cache.set('some_key', response.content, 300)
        else:
            response = HttpResponse(response_data)
        return response
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this doesn't answer the question as this only shows how to cache & retrieve raw data. The work that cache_page() does for you in terms of caching the entire response and setting headers is lost here.
to set cache_key Use: cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache) and to set headers use: patch_response_headers(response, timeout). refer UpdateCacheMiddleware class

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.