From c4e95cf5abd912fa9e11e44933cfe437ba15a249 Mon Sep 17 00:00:00 2001 From: Volodymir Shlikhta Date: Thu, 5 Dec 2024 20:01:51 +0200 Subject: [PATCH] my decorator cash --- app/main.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..c95902bc 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,19 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_data = {} + + def wrapper(*args, **kwargs): + key = (args, frozenset(kwargs.items())) + + if key in cache_data: + print("Getting from cache") + return cache_data[key] + + print("Calculating new result") + result = func(*args, **kwargs) + cache_data[key] = result + return result + + return wrapper +