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

Refactor default lock #322

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

bmwant
Copy link

@bmwant bmwant commented Aug 18, 2024

Description

Make cached and cachedmethod functions more concise by introducing default no-op NoLock class thus eliminating duplication in elif lock is None: branches.

Testing

import timeit
setup = """
from cachetools import cached
import time

@cached(cache={})
def dummy_func():
    time.sleep(0.1)
    return 10 ** 10
"""

s = """
dummy_func()
"""

print(timeit.timeit(setup=setup, stmt=s, number=100_000))
# 0.14263679101713933
# 0.15603854099754244

There is no significant overhead with running no-op locking/unlocking (only 1/100th of a second) and taking into account that caching is always applied to already "heavy" functions it's negligible.

@bmwant
Copy link
Author

bmwant commented Aug 18, 2024

@tkem let me know what do you think about this change, in my opinion it improves readability and makes it easier to maintain/update in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant