From 3ea2f411eca2d5afff096fa1539c7b4e7c4f3e44 Mon Sep 17 00:00:00 2001 From: Yurii Prydii Date: Mon, 18 Nov 2024 23:24:20 +0200 Subject: [PATCH] Solution --- app/main.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..b4daf2af 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,18 @@ -from typing import Callable +from typing import Callable, Any, Tuple, Dict def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_storage: Dict[Tuple, Any] = {} + + def inner(*args, **kwargs) -> Callable: + key = (args, frozenset(kwargs.items())) + if key in cache_storage: + print("Getting from cache") + return cache_storage[key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_storage[key] = result + return result + + return inner