From 12ab764d1ad8fc7a03593b4cb2b21a64d1de11ea Mon Sep 17 00:00:00 2001 From: Taras Date: Thu, 21 Nov 2024 18:22:33 +0200 Subject: [PATCH] Cache decorator function for reviewing --- app/main.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..329583a9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,21 @@ from typing import Callable +from functools import wraps def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_storage = {} + + @wraps(func) + def wrapper(*args) -> dict: + if isinstance(args, (str, int, float, tuple, bool)): + if args in cache_storage: + print("Getting from cache") + return cache_storage[args] + else: + print("Calculating new result") + result = func(*args) + cache_storage[args] = result + return result + else: + print("Not immutable data") + return wrapper