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/05 14:47:58 UTC

[GitHub] [incubator-superset] DiggidyDave commented on a change in pull request #7227: Improve cache

DiggidyDave commented on a change in pull request #7227: Improve cache
URL: https://github.com/apache/incubator-superset/pull/7227#discussion_r272621430
 
 

 ##########
 File path: superset/utils/decorators.py
 ##########
 @@ -61,32 +62,49 @@ 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()
-                expiration = max_age if max_age != 0 else FAR_FUTURE
-                response.expires = response.last_modified + timedelta(seconds=expiration)
-                response.add_etag()
+            response = None
+
+            # if this is a GET request and we have a cache, try to fetch a
+            # cached response object
+            if cache and request.method == 'GET':
                 try:
-                    cache.set(cache_key, response, timeout=max_age)
+                    # 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.')
 
+            # if this is not a GET, or if no response was cached, compute the
+            # response using the wrapped function
+            if response is None:
+                response = f(*args, **kwargs)
+
+                # if this was a GET request, add headers that help with
+                # caching: Last Modified, Expires and ETag
+                if request.method == 'GET':
 
 Review comment:
   I agree that it would read more clearly if the GET/POST semantics were cleanly separated rather than interleaved.
   I personally would prefer two separate methods servicing those independently but opinions may vary of course. Any duplication would imply refactoring of code into functions.
   
   Like this would be much more clear IMO:
   ```
   resp = None
   if request.method == 'GET':
     if cache:
       resp = try_read_cache()
   
     if not resp:
       resp = compute_response(...)
       # add modified/expires/etag headers
   
   elif request.method == 'POST':
       resp = compute_response(...)
   fi
   return resp
   ```
   
   Incidentially, now that it's broken down that way I realize I have a question: where are set setting the cache for POST requests?

----------------------------------------------------------------
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