You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2019/04/02 21:24:10 UTC

[GitHub] [incubator-superset] betodealmeida commented on a change in pull request #7032: Fetch charts with GET to benefit from browser cache and conditional requests

betodealmeida commented on a change in pull request #7032: Fetch charts with GET to benefit from browser cache and conditional requests
URL: https://github.com/apache/incubator-superset/pull/7032#discussion_r271501724
 
 

 ##########
 File path: superset/utils/decorators.py
 ##########
 @@ -29,3 +35,60 @@ def stats_timing(stats_key, stats_logger):
         raise e
     finally:
         stats_logger.timing(stats_key, now_as_float() - start_ts)
+
+
+def etag_cache(max_age, check_perms=bool):
+    """
+    A decorator for caching views and handling etag conditional requests.
+
+    The decorator caches the response, returning headers for etag and last
+    modified. If the client makes a request that matches, the server will
+    return a "304 Not Mofified" status.
+
+    If no cache is set, the decorator will still set the ETag header, and
+    handle conditional requests.
+
+    """
+    def decorator(f):
+        @wraps(f)
+        def wrapper(*args, **kwargs):
+            # check if the user can access the resource
+            check_perms(*args, **kwargs)
+
+            try:
+                # build the cache key from the function arguments and any other
+                # additional GET arguments (like `form_data`, eg).
+                key_args = list(args)
+                key_kwargs = kwargs.copy()
+                key_kwargs.update(request.args)
+                cache_key = wrapper.make_cache_key(f, *key_args, **key_kwargs)
+                response = cache.get(cache_key)
+            except Exception:  # pylint: disable=broad-except
+                if app.debug:
+                    raise
+                logging.exception('Exception possibly due to cache backend.')
+                response = None
+
+            if response is None or request.method == 'POST':
+                response = f(*args, **kwargs)
+                response.cache_control.public = True
+                response.last_modified = datetime.utcnow()
+                response.expires = response.last_modified + timedelta(seconds=max_age)
+                response.add_etag()
+                try:
+                    cache.set(cache_key, response, timeout=max_age)
 
 Review comment:
   @john-bodley good point. I'll fix this.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org