-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
16 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
from typing import Callable | ||
from typing import Callable, Any | ||
from functools import wraps | ||
|
||
|
||
def cache(func: Callable) -> Callable: | ||
# Write your code here | ||
pass | ||
cache_storage = {} | ||
|
||
@wraps(func) | ||
def wrapper(*args, **kwargs) -> Any: | ||
cache_key = (args, tuple(sorted(kwargs.items()))) | ||
if cache_key in cache_storage: | ||
print("Getting from cache") | ||
return cache_storage[cache_key] | ||
else: | ||
print("Calculating new result") | ||
result = func(*args, **kwargs) | ||
cache_storage[cache_key] = result | ||
return result | ||
return wrapper |