Skip to content

Conversation

@RayBB
Copy link
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
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
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