diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 065264c..98b977d 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.10.0 +current_version = 1.10.1 commit = False tag = False parse = (?P\d+)\.(?P\d+)\.(?P\d+)(\-(?P[a-z]+)(?P\d+))? diff --git a/nwastdlib/__init__.py b/nwastdlib/__init__.py index 6253744..4509678 100644 --- a/nwastdlib/__init__.py +++ b/nwastdlib/__init__.py @@ -13,7 +13,7 @@ # """The NWA-stdlib module.""" -__version__ = "1.10.0" +__version__ = "1.10.1" from nwastdlib.f import const, identity diff --git a/nwastdlib/asyncio_cache.py b/nwastdlib/asyncio_cache.py index 22d7653..373f360 100644 --- a/nwastdlib/asyncio_cache.py +++ b/nwastdlib/asyncio_cache.py @@ -117,13 +117,13 @@ async def get_signed_cache_value(pool: AIORedis, secret: str, cache_key: str, se return _deserialize(pickled_value, serializer) -def _generate_cache_key_suffix(*, skip_first: bool, args: tuple, kwargs: dict) -> str: +def _generate_cache_key_suffix(*, skip_first: bool, args: tuple, kwargs: dict) -> str | None: # Auto generate cache key suffix based on the arguments # Note: this makes no attempt to handle non-hashable values like lists and sets or other complex objects filtered_args = args[int(skip_first) :] filtered_kwargs = frozenset(kwargs.items()) if not filtered_args and not filtered_kwargs: - raise ValueError("Cannot generate cache key without args/kwargs") + return None args_and_kwargs_string = (filtered_args, filtered_kwargs) return str(args_and_kwargs_string) @@ -244,6 +244,7 @@ def my_other_function... def cache_decorator(func: Callable) -> Callable: _validate_coroutine(func) skip_first = _validate_signature(func) + prefix_version_func = f"{prefix_version}:{func.__qualname__}".lower() @wraps(func) async def func_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any: @@ -251,9 +252,10 @@ async def func_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any: if static_cache_key: cache_key = static_cache_key + elif suffix := _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs): + cache_key = f"{prefix_version_func}:{suffix}" else: - suffix = _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs) - cache_key = f"{prefix_version}:{func.__name__}:{suffix}" + cache_key = prefix_version_func if from_cache: logger.debug("Cache called with wrapper func", func_name=func.__name__, cache_key=cache_key) diff --git a/tests/test_asyncio_cache.py b/tests/test_asyncio_cache.py index 174f004..4865a3c 100644 --- a/tests/test_asyncio_cache.py +++ b/tests/test_asyncio_cache.py @@ -306,18 +306,8 @@ async def foo(param: str): {}, "((UUID('12345678-0000-1111-2222-0123456789ab'),), frozenset())", ), + (False, (), {}, None), ], ) def test_generate_cache_key_suffix(skip_first, args, kwargs, expected_key): assert _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs) == expected_key - - -@pytest.mark.parametrize( - ("skip_first", "args", "kwargs", "expected_exception"), - [ - (False, (), {}, ValueError), - ], -) -def test_generate_cache_key_errors(skip_first, args, kwargs, expected_exception): - with pytest.raises(expected_exception): - _generate_cache_key_suffix(skip_first=skip_first, args=args, kwargs=kwargs)