Exemplo n.º 1
0
    def _wrapped_view_func(request, *args, **kwargs):
        if cr_settings['CACHE_PAGES']:

            # check if request is cacheable
            request.is_preview = getattr(request, 'is_preview', False)
            is_cacheable = request.method in (
                'GET', 'HEAD'
            ) and not request.is_preview and not request.user.is_authenticated
            for fn in hooks.get_hooks('is_request_cacheable'):
                result = fn(request)
                if isinstance(result, bool):
                    is_cacheable = result

            if is_cacheable:
                cache = caches[cr_settings['CACHE_BACKEND']]
                djcache = CacheMiddleware(
                    cache_alias=cr_settings['CACHE_BACKEND'],
                    cache_timeout=cache.
                    default_timeout,  # override CacheMiddleware's default timeout
                    key_prefix=None)
                response = djcache.process_request(request)
                if response:
                    response['X-Crcms-Cache'] = 'hit'
                    return response

                # since we don't have a response at this point, run the view.
                response = view_func(request, *args, **kwargs)
                response['X-Crcms-Cache'] = 'miss'
                djcache.process_response(request, response)

                return response

        # as a fall-back, just run the view function.
        return view_func(request, *args, **kwargs)
Exemplo n.º 2
0
def thumbnail(request, board_id, marked=False, voted=False):
    bingo_board = get_object_or_404(BingoBoard,
                                    board_id=board_id,
                                    game__site=get_current_site(request))
    square = bool(request.GET.get("square"))
    large = bool(request.GET.get("large"))

    # check if the board is from an expired game
    game_expired_cachename = "game_expired__board={0:d}".format(
        int(bingo_board.id))
    game_expired = cache.get(game_expired_cachename)
    if game_expired is None:
        game_expired = bingo_board.game.is_expired()
        cache.set(game_expired_cachename, game_expired, 60 * 60)

    # when the game of the board is expired,
    # the thumbnail can be cached longer.
    if game_expired:
        m = CacheMiddleware(cache_timeout=OLD_THUMBNAIL_CACHE_EXPIRY,
                            key_prefix="site_id={0:d}".format(
                                get_current_site(request).id))
    else:
        m = CacheMiddleware(cache_timeout=THUMBNAIL_CACHE_EXPIRY,
                            key_prefix="site_id={0:d}".format(
                                get_current_site(request).id))

    response = m.process_request(request)
    if response:  # cached
        return response

    response = HttpResponse(content_type="image/png")
    filename = _get_image_name(board_id, marked, voted) + \
        "_" + _("thumbnail") + ".png"
    response['Content-Disposition'] = 'filename={0}'.format(filename)
    # TODO: replace hardcoded values with configurable ones
    kwargs = {}
    if large:
        kwargs = {'thumbnail_width': 500, 'thumbnail_height': 500}
    im = image_module.get_thumbnail(request.get_host(),
                                    bingo_board,
                                    marked,
                                    voted,
                                    square=square,
                                    **kwargs)
    im.save(response, "png")

    return m.process_response(request, response)
Exemplo n.º 3
0
def process_request(req, prefix, cache_time=60 * 60):
    # retrieve the cache using the django's CacheMiddleware
    cache_middleware = CacheMiddleware(cache_timeout=cache_time,
                                       key_prefix=prefix)
    response = cache_middleware.process_request(req)
    # if no cache is found, return false
    if not response:
        return False
    return response
Exemplo n.º 4
0
def process_response(req, res, prefix, cache_time=60 * 60):
    # update the cache using the django's CacheMiddleware
    cache_middleware = CacheMiddleware(cache_timeout=cache_time,
                                       key_prefix=prefix)
    response = cache_middleware.process_response(req, res)
    # update some header to prevent wrong client caching
    max_age = get_max_age(response)
    if max_age and max_age < max_age:
        # Remove headers so patch_response works
        for header in ('ETag', 'Last-Modified', 'Expires'):
            if response.has_header(header):
                del response[header]
        patch_response_headers(response, max_age)
    return response
Exemplo n.º 5
0
    def _wrapped_view_func(request, *args, **kwargs):
        if wagtailcache_settings['WAGTAIL_CACHE']:

            # check if request is cacheable
            request.is_preview = getattr(request, 'is_preview', False)
            is_cacheable = request.method in ('GET', 'HEAD') and \
                not request.is_preview and \
                not request.user.is_authenticated
            for fn in hooks.get_hooks('is_request_cacheable'):
                result = fn(request)
                if isinstance(result, bool):
                    is_cacheable = result

            if is_cacheable:
                cache = caches[wagtailcache_settings['WAGTAIL_CACHE_BACKEND']]
                # Create a CacheMiddleware using our specified values
                djcache = CacheMiddleware(
                    cache_alias=wagtailcache_settings['WAGTAIL_CACHE_BACKEND'],
                    cache_timeout=cache.default_timeout,
                    key_prefix=None)
                response = djcache.process_request(request)
                if response:
                    # add a response header to indicate this was a cache hit
                    if wagtailcache_settings['WAGTAIL_CACHE_HEADER']:
                        response[wagtailcache_settings[
                            'WAGTAIL_CACHE_HEADER']] = 'hit'
                    return response

                # since we don't have a response at this point, run the view.
                response = view_func(request, *args, **kwargs)
                # add a response header to indicate this was a cache miss
                if wagtailcache_settings['WAGTAIL_CACHE_HEADER']:
                    response[
                        wagtailcache_settings['WAGTAIL_CACHE_HEADER']] = 'miss'
                djcache.process_response(request, response)

                return response

        # as a fall-back, just run the view function.
        return view_func(request, *args, **kwargs)
Exemplo n.º 6
0
 def set_cache_middleware(self, cache_time, prefix):
     name = router.router.get_cache_name(prefix=prefix)
     self.cache_middleware = CacheMiddleware(cache_timeout=cache_time,
                                               key_prefix=prefix,
                                             cache_alias=name)
Exemplo n.º 7
0
 def set_cache_middleware(self, cache_time, prefix):
     self.cache_middleware = CacheMiddleware(cache_timeout=cache_time,
                                             key_prefix=prefix)