remove method_memoize to DRY caching #11684
Open
+5
−65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Part of #11479
Some of this text was AI generated but I've closely guided and monitored the changes.
Remove
method_memoizeand Replace with Standard Python CachingSummary
This PR removes the custom
method_memoizedecorator from the codebase and replaces it with standard Python caching patternsfunctools.cached_property.Changes
Production Code
openlibrary/core/models.py:@cache.method_memoizewith@functools.cached_propertyon:Thing.get_history_preview()Work.edition_countget_history_previewas a property instead of a method callWhy It's Safe
What it does:
functools.cached_propertycaches the result in the instance's__dict__on first access. Subsequent accesses return the cached value without recomputing.Why it's safe:
method_memoizefor methods with no argumentsPerformance Considerations
As far as I know, there is no risk of performance changes.
Testing
Poking around the local website and looking at history and editions everything seems fine.
Stakeholders
@cdrini