Search code examples
djangopython-decoratorsdjango-middlewaredjango-cachedjango-cache-machine

Django custom cache_page decorater returns error on the only first run afterwards it's ok


I created custom cache_page decorator for my app. It doesnt work on the first run and throwing error related to middleware:

content_encoding = response.get("Content-Encoding", "")

AttributeError: 'bool' object has no attribute 'get'

But on the second and further run, it works because the cache has been set. I installed django debug_toolbar and added cors middleware to my middlewares. Can anyone help on this? Here is my custom decorator func:

def cache_page(timeout):
    """custom cache page decorator"""
    def decorator(func):
        @wraps(func)
        def wrapper(request, *args, **kwargs):
            print("wrapp", request)
            cache_key = hashlib.md5(
                iri_to_uri(request.build_absolute_uri()).encode('ascii')
            ).hexdigest()
            cached_data = cache.get(cache_key)
            if cached_data is not None:
                return cached_data
            response = func(request, *args, **kwargs)
            if (isinstance(response, Response) and response.status_code in (200, 301, 302, 304)):
                cache_timeout = timeout() if callable(timeout) else timeout
                if hasattr(response, 'render') and callable(response.render):
                    response.add_post_render_callback(
                        lambda r: cache.set(cache_key, r, cache_timeout)
                    )
                else:
                    cache.set(cache_key, response, cache_timeout)
            return response
        return wrapper
    return decorator

Solution

  • Tested with Django 2.2b:

    I used this Mixin:

    class CacheKeyDispatchMixin:
        def dispatch(self, *args, **kwargs):
            if self.request.method == 'GET' or self.request.method == 'HEAD':
                url_to_cache = '/{0}{1}'.format(get_language(), self.request.get_full_path())
                cache_hash = calculate_xxxhash(url_to_cache)
                data = cache.get(cache_hash)
                if not data:
                    response = super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
                    if response.status_code == 200:
                        response.render()
                        cache.set(cache_hash, response)
                        logger.info('Cache added {0} ({1})'.format(url_to_cache, cache_hash))
                    return response
                logger.info('Cache hit {0} ({1}).'.format(url_to_cache, cache_hash))
                return data
    
            return super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
    

    Basically you can call the render() before caching it.