fix: revert eTag cache feature for dashboard (#11203)

* revert #11137

* revert #10963
This commit is contained in:
Grace Guo
2020-10-08 12:15:08 -07:00
committed by GitHub
parent b6728d87a0
commit a10e86ab31
5 changed files with 50 additions and 154 deletions

View File

@@ -17,7 +17,7 @@
import logging
from datetime import datetime, timedelta
from functools import wraps
from typing import Any, Callable, Iterator, Optional
from typing import Any, Callable, Iterator
from contextlib2 import contextmanager
from flask import request
@@ -46,13 +46,7 @@ def stats_timing(stats_key: str, stats_logger: BaseStatsLogger) -> Iterator[floa
stats_logger.timing(stats_key, now_as_float() - start_ts)
def etag_cache(
max_age: int,
check_perms: Callable[..., Any],
get_last_modified: Optional[Callable[..., Any]] = None,
skip: Optional[Callable[..., Any]] = None,
must_revalidate: Optional[bool] = False,
) -> Callable[..., Any]:
def etag_cache(max_age: int, check_perms: Callable[..., Any]) -> Callable[..., Any]:
"""
A decorator for caching views and handling etag conditional requests.
@@ -75,12 +69,10 @@ def etag_cache(
# for POST requests we can't set cache headers, use the response
# cache nor use conditional requests; this will still use the
# dataframe cache in `superset/viz.py`, though.
if request.method == "POST" or (skip and skip(*args, **kwargs)):
if request.method == "POST":
return f(*args, **kwargs)
response = None
last_modified = get_last_modified and get_last_modified(*args, **kwargs)
if cache:
try:
# build the cache key from the function arguments and any
@@ -97,37 +89,17 @@ def etag_cache(
raise
logger.exception("Exception possibly due to cache backend.")
# if cache is stale?
if (
response
and last_modified
and response.last_modified
and response.last_modified < last_modified
):
response = None
# if no response was cached, compute it using the wrapped function
if response is None:
# if no response was cached, compute it using the wrapped function
response = f(*args, **kwargs)
# set expiration headers:
# Last-Modified, Expires, Cache-Control, ETag
response.last_modified = last_modified or datetime.utcnow()
# add headers for caching: Last Modified, Expires and ETag
response.cache_control.public = True
response.last_modified = datetime.utcnow()
expiration = max_age if max_age != 0 else FAR_FUTURE
response.expires = response.last_modified + timedelta(
seconds=expiration
)
# when needed, instruct the browser to always revalidate cache
if must_revalidate:
# `Cache-Control: no-cache` asks the browser to always store
# the cache, but also must validate it with the server.
response.cache_control.no_cache = True
else:
# `Cache-Control: Public` asks the browser to always store
# the cache.
response.cache_control.public = True
response.add_etag()
# if we have a cache, store the response from the request