From 92d5f8860bf06647a57892056e42b8ff35449f4d Mon Sep 17 00:00:00 2001 From: Andrey Dementyev Date: Sat, 5 Oct 2024 20:38:33 +0300 Subject: [PATCH] 'Solution' --- app/main.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..bca34f78 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,20 @@ from typing import Callable +from functools import wraps def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + @wraps(func) + def wrapper(*args, **kwargs) -> Callable: + key = (args, tuple(kwargs.items())) + + if key in cache_dict: + print("Getting from cache") + return cache_dict[key] + else: + print("Calculating new result") + cache_dict[key] = func(*args, **kwargs) + return cache_dict[key] + + return wrapper