From 525b204e64100b4be6fa75ea1347dbae111d4511 Mon Sep 17 00:00:00 2001 From: Psychox1k Date: Fri, 1 Nov 2024 18:59:18 +0100 Subject: [PATCH] Solution cache decorator --- app/main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..4dc85881 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,20 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + storage_cache = {} + + def wrapper(*args) -> Any: + + key = args + + if key in storage_cache: + print("Getting from cache") + return storage_cache[key] + + print("Calculating new result") + result = func(*args) + storage_cache[key] = result + return result + + return wrapper