From 90e210892ba5c076b47ab3007c7e027e9b405b6b Mon Sep 17 00:00:00 2001 From: Sebastian Liebscher <112352529+sebastianliebscher@users.noreply.github.com> Date: Wed, 8 Nov 2023 21:57:40 +0100 Subject: [PATCH] chore: Simplify utils/cache by using default argument values (#25900) --- superset/utils/cache.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/superset/utils/cache.py b/superset/utils/cache.py index 693f3a73bcfe5..48e283e7c11cd 100644 --- a/superset/utils/cache.py +++ b/superset/utils/cache.py @@ -89,14 +89,7 @@ def set_and_log_cache( logger = logging.getLogger(__name__) -def view_cache_key(*args: Any, **kwargs: Any) -> str: # pylint: disable=unused-argument - args_hash = hash(frozenset(request.args.items())) - return f"view/{request.path}/{args_hash}" - - -def memoized_func( - key: str | None = None, cache: Cache = cache_manager.cache -) -> Callable[..., Any]: +def memoized_func(key: str, cache: Cache = cache_manager.cache) -> Callable[..., Any]: """ Decorator with configurable key and cache backend. @@ -129,14 +122,11 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any: if not kwargs.get("cache", True): return f(*args, **kwargs) - if key: - # format the key using args/kwargs passed to the decorated function - signature = inspect.signature(f) - bound_args = signature.bind(*args, **kwargs) - bound_args.apply_defaults() - cache_key = key.format(**bound_args.arguments) - else: - cache_key = view_cache_key(*args, **kwargs) + # format the key using args/kwargs passed to the decorated function + signature = inspect.signature(f) + bound_args = signature.bind(*args, **kwargs) + bound_args.apply_defaults() + cache_key = key.format(**bound_args.arguments) obj = cache.get(cache_key) if not kwargs.get("force") and obj is not None: @@ -153,7 +143,7 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any: def etag_cache( cache: Cache = cache_manager.cache, get_last_modified: Callable[..., datetime] | None = None, - max_age: int | float | None = None, + max_age: int | float = app.config["CACHE_DEFAULT_TIMEOUT"], raise_for_access: Callable[..., Any] | None = None, skip: Callable[..., bool] | None = None, ) -> Callable[..., Any]: @@ -169,8 +159,6 @@ def etag_cache( dataframe cache for requests that produce the same SQL. """ - if max_age is None: - max_age = app.config["CACHE_DEFAULT_TIMEOUT"] def decorator(f: Callable[..., Any]) -> Callable[..., Any]: @wraps(f)