From f4ed3949728c13e4bf9707b4dbc669a40f419ad4 Mon Sep 17 00:00:00 2001 From: Ngayka Date: Sat, 30 Nov 2024 16:51:25 +0200 Subject: [PATCH] Solution whith Any type --- app/main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..29e7f312 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,19 @@ -from typing import Callable +from typing import Callable, Any +from functools import wraps def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_storage = {} + + @wraps(func) + def wrapper(*args, **kwargs) -> Any: + cache_key = (args, tuple(sorted(kwargs.items()))) + if cache_key in cache_storage: + print("Getting from cache") + return cache_storage[cache_key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_storage[cache_key] = result + return result + return wrapper