Optimize Harvests with memoization and fragment caching#4595
Conversation
- Memoize display methods in `Harvest` model. - Memoize calculation methods in `PredictHarvest` concern using `defined?` for nil safety. - Add fragment caching to `app/views/harvests/_popover.html.haml`. - Add fragment caching to `app/views/crops/_harvests.html.haml` with daily expiration for relative time strings. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
| @@ -6,13 +6,14 @@ | |||
| - unless crop.harvests.empty? | |||
| %ul.list-group.list-group-flush | |||
| - Harvest.where(crop: crop).includes(:owner).order(harvested_at: :desc).limit(3).each do |harvest| | |||
There was a problem hiding this comment.
@jules cache this query as well, unless the whole partial is already cached
There was a problem hiding this comment.
I've wrapped the query in Rails.cache.fetch with a key that includes the crop and the current date (to keep relative time strings accurate). I've also kept the inner fragment cache for each individual harvest item for maximum reuse across different views.
- Memoize display methods in `Harvest` model. - Memoize calculation methods in `PredictHarvest` concern using `defined?` for nil safety. - Add fragment caching to `app/views/harvests/_popover.html.haml`. - Add fragment caching and query caching to `app/views/crops/_harvests.html.haml` with daily expiration for relative time strings. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>
Implemented memoization for display and calculation methods in the
Harvestmodel andPredictHarvestconcern to reduce redundant processing. Added fragment caching for harvest-related partials, including a strategy to prevent stale relative time strings by including the current date in the cache key.PR created automatically by Jules for task 9176733581563564983 started by @CloCkWeRX