Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Simplify utils/cache by using default argument values #25900

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines -132 to -139
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memoized_func() has no case with key = None so unnecessary view_cache_key() can be removed.

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