From c45c358d2f8c0b68a9168a196a8dc0555b4ead70 Mon Sep 17 00:00:00 2001 From: Artem_Shatylo Date: Sun, 6 Oct 2024 19:47:51 +0300 Subject: [PATCH] Solution --- app/main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..ed951cd2 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,20 @@ -from typing import Callable +from functools import wraps +from typing import Callable, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + @wraps(func) + def wrapper(*args: Any, **kwargs: Any) -> Any: + key = (func.__name__, args, frozenset(sorted(kwargs.items()))) + if key in cache_dict: + print("Getting from cache") + return cache_dict[key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_dict[key] = result + return result + + return wrapper