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

This commit is contained in:
Sebastian Liebscher 2023-11-08 21:57:40 +01:00 committed by GitHub
parent e03691917b
commit 90e210892b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 19 deletions

View File

@ -89,14 +89,7 @@ ONE_YEAR = 365 * 24 * 60 * 60 # 1 year in seconds
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 memoized_func(
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 memoized_func(
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)