diff --git a/src/algorithms/caching.py b/src/algorithms/caching.py index 2adf488..7e6f05f 100644 --- a/src/algorithms/caching.py +++ b/src/algorithms/caching.py @@ -6,12 +6,16 @@ def time_based_cache(expiry_seconds: int) -> Callable: """Manual implementation of a time-based cache decorator.""" def decorator(func: Callable) -> Callable: - cache: dict[str, tuple[Any, float]] = {} + cache: dict[tuple, tuple[Any, float]] = {} + + def make_key(args, kwargs) -> tuple: + if kwargs: + items = tuple(sorted(kwargs.items())) + return (args, items) + return (args, None) def wrapper(*args, **kwargs) -> Any: - key_parts = [repr(arg) for arg in args] - key_parts.extend(f"{k}:{repr(v)}" for k, v in sorted(kwargs.items())) - key = ":".join(key_parts) + key = make_key(args, kwargs) current_time = time.time()