From dc85bd41a970066abde5e93603bf66a3db80614c Mon Sep 17 00:00:00 2001 From: Beliar24 Date: Sat, 2 Nov 2024 00:59:35 +0200 Subject: [PATCH] cache function implemented --- app/main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..08678159 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,18 @@ +from functools import wraps from typing import Callable def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + @wraps(func) + def wrapper(*args, **kwargs): + if args in cache_dict: + print("Getting from cache") + return cache_dict[args] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_dict[args] = result + return result + return wrapper