def get_or_create( request_data, cache, load_data, render_page, get_cache_key, get_etag_key, get_response, debug, request): """ Returns an archived version of a document. The response is cached and ETags are handled. """ if debug: # do not cache when in debug mode _, _, loaded_data = load_data() return get_response(render_page(*loaded_data)) cache_key = get_cache_key(*request_data) # try to get a rendered page from the cache cached_page = cache_get(cache, cache_key) old_api_cache_key = cached_page.api_cache_key \ if cached_page != NO_VALUE else None # request the document from the api. if there was an entry in the # cache, set the `If-None-Match` header with the last ETag. if the # document version on the api has not changed, `not modified` will # be returned. not_modified, api_cache_key, loaded_data = load_data(old_api_cache_key) ui_etag_key = get_etag_key(api_cache_key) if not_modified: # the cached page is still valid log.debug('Serving from cache {0}'.format(cache_key)) etag_cache(request, ui_etag_key) return get_response(cached_page.page_html) else: # there is a new version from the api, render the page page_html = render_page(*loaded_data) cache_set(cache, cache_key, CachedPage(api_cache_key, page_html)) etag_cache(request, ui_etag_key) return get_response(page_html)
def get_or_create(request_data, cache, load_data, render_page, get_cache_key, get_etag_key, get_response, debug, request): """ Returns an archived version of a document. The response is cached and ETags are handled. """ if debug: # do not cache when in debug mode _, _, loaded_data = load_data() return get_response(render_page(*loaded_data)) cache_key = get_cache_key(*request_data) # try to get a rendered page from the cache cached_page = cache_get(cache, cache_key) old_api_cache_key = cached_page.api_cache_key \ if cached_page != NO_VALUE else None # request the document from the api. if there was an entry in the # cache, set the `If-None-Match` header with the last ETag. if the # document version on the api has not changed, `not modified` will # be returned. not_modified, api_cache_key, loaded_data = load_data(old_api_cache_key) ui_etag_key = get_etag_key(api_cache_key) if not_modified: # the cached page is still valid log.debug('Serving from cache {0}'.format(cache_key)) etag_cache(request, ui_etag_key) return get_response(cached_page.page_html) else: # there is a new version from the api, render the page page_html = render_page(*loaded_data) cache_set(cache, cache_key, CachedPage(api_cache_key, page_html)) etag_cache(request, ui_etag_key) return get_response(page_html)
def get_response_html(page_html): return get_response(self.request, page_html)