Skip to content

Commit

Permalink
revert apache#11137
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace committed Oct 8, 2020
1 parent b6728d8 commit 0fa081f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
34 changes: 13 additions & 21 deletions superset/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def etag_cache(
check_perms: Callable[..., Any],
get_last_modified: Optional[Callable[..., Any]] = None,
skip: Optional[Callable[..., Any]] = None,
must_revalidate: Optional[bool] = False,
) -> Callable[..., Any]:
"""
A decorator for caching views and handling etag conditional requests.
Expand Down Expand Up @@ -79,8 +78,6 @@ def wrapper(*args: Any, **kwargs: Any) -> ETagResponseMixin:
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
Expand All @@ -97,37 +94,32 @@ def wrapper(*args: Any, **kwargs: Any) -> ETagResponseMixin:
raise
logger.exception("Exception possibly due to cache backend.")

# if cache is stale?
# if cache is stale?
if get_last_modified:
content_changed_time = get_last_modified(*args, **kwargs)
if (
response
and last_modified
and response.last_modified
and response.last_modified < last_modified
and response.last_modified.timestamp()
< content_changed_time.timestamp()
):
response = None
else:
# if caller didn't provide content's last_modified time, assume
# its cache won't be stale.
content_changed_time = datetime.utcnow()

# 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 = content_changed_time
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
Expand Down
8 changes: 0 additions & 8 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,6 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods

logger = logging.getLogger(__name__)

def __repr__(self) -> str:
"""Determinate string representation of the view instance for etag_cache."""
return "Superset.views.core.Superset@v{}{}".format(
self.appbuilder.app.config["VERSION_STRING"],
self.appbuilder.app.config["VERSION_SHA"],
)

@has_access_api
@expose("/datasources/")
def datasources(self) -> FlaskResponse:
Expand Down Expand Up @@ -1612,7 +1605,6 @@ def publish( # pylint: disable=no-self-use
skip=lambda _self, dashboard_id_or_slug: not is_feature_enabled(
"ENABLE_DASHBOARD_ETAG_HEADER"
),
must_revalidate=True,
)
@expose("/dashboard/<dashboard_id_or_slug>/")
def dashboard( # pylint: disable=too-many-locals
Expand Down

0 comments on commit 0fa081f

Please sign in to comment.