Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution cache decorator #1380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation uses the args tuple directly as the cache key. This can lead to issues if the arguments are not hashable (e.g., lists). Consider converting the arguments to a hashable type or ensuring that only hashable types are used as arguments.

if key in storage_cache:
print("Getting from cache")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check if key in storage_cache assumes that the args tuple is a valid key. Ensure that all elements in args are hashable, or modify the key generation to handle non-hashable types.

return storage_cache[key]

print("Calculating new result")
result = func(*args)
storage_cache[key] = result
return result

return wrapper
Loading