Skip to content

remove method_memoize to DRY caching#11684

Merged
cdrini merged 3 commits intomasterfrom
refactor/11479-remove-method_memoize
Feb 11, 2026
Merged

remove method_memoize to DRY caching#11684
cdrini merged 3 commits intomasterfrom
refactor/11479-remove-method_memoize

Conversation

@RayBB
Copy link
Copy Markdown
Collaborator

@RayBB RayBB commented Jan 8, 2026

Part of #11479

Some of this text was AI generated but I've closely guided and monitored the changes.

Remove method_memoize and Replace with Standard Python Caching

Summary

This PR removes the custom method_memoize decorator from the codebase and replaces it with standard Python caching patterns functools.cached_property.

Changes

Production Code

  1. openlibrary/core/models.py:
    • Replace @cache.method_memoize with @functools.cached_property on:
      • Thing.get_history_preview()
      • Work.edition_count
    • Update callers to access get_history_preview as a property instead of a method call

Why It's Safe

What it does: functools.cached_property caches the result in the instance's __dict__ on first access. Subsequent accesses return the cached value without recomputing.

Why it's safe:

  • Same behavior: Functionally equivalent to method_memoize for methods with no arguments
  • Standard library: Part of Python 3.8+, well-tested and documented

Performance 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

@RayBB RayBB marked this pull request as ready for review January 9, 2026 00:06
@RayBB RayBB mentioned this pull request Jan 9, 2026
1 task
@RayBB
Copy link
Copy Markdown
Collaborator Author

RayBB commented Jan 9, 2026

@cdrini I'm fairly confident in this PR but I'd love a second set of eyes on this by you!

@RayBB RayBB requested a review from cdrini January 9, 2026 00:07
@RayBB RayBB mentioned this pull request Jan 9, 2026
Co-authored-by: Drini Cami <cdrini@gmail.com>
@RayBB RayBB requested a review from cdrini January 21, 2026 19:02
@RayBB
Copy link
Copy Markdown
Collaborator Author

RayBB commented Jan 21, 2026

@cdrini rename done

Copy link
Copy Markdown
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

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

Lgtm! Tested on testing. Woohoo, another duplicate caching function down!

@cdrini cdrini merged commit bcea3fa into master Feb 11, 2026
8 checks passed
@cdrini cdrini deleted the refactor/11479-remove-method_memoize branch February 11, 2026 18:53
@RayBB RayBB removed the On Testing label Feb 11, 2026
bhardwajparth51 added a commit to bhardwajparth51/openlibrary that referenced this pull request Feb 17, 2026
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.

2 participants