Skip to content

Commit

Permalink
chore: Simplify utils/cache by using default argument values (apache#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianliebscher authored and sfirke committed Mar 22, 2024
1 parent bd1fb0f commit 62697dc
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions superset/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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]:
Expand All @@ -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)
Expand Down

0 comments on commit 62697dc

Please sign in to comment.