Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

perf: memoize Family#balance_sheet and sync status lookups per request (#2553)

Juan José Mata4d1d33f91da3
diff --git a/app/models/balance_sheet/sync_status_monitor.rb b/app/models/balance_sheet/sync_status_monitor.rb
index 475454eecd..a1f9fb2595 100644
--- a/app/models/balance_sheet/sync_status_monitor.rb
+++ b/app/models/balance_sheet/sync_status_monitor.rb
@@ -15,7 +15,7 @@ class BalanceSheet::SyncStatusMonitor
attr_reader :family
def syncing_account_ids
- Rails.cache.fetch(cache_key) do
+ @syncing_account_ids ||= Rails.cache.fetch(cache_key) do
Sync.visible
.where(syncable_type: "Account", syncable_id: family.accounts.visible.pluck(:id))
.pluck(:syncable_id)
diff --git a/app/models/family.rb b/app/models/family.rb
index c2ae27dca7..97aa264d56 100644
--- a/app/models/family.rb
+++ b/app/models/family.rb
@@ -330,8 +330,12 @@ class Family < ApplicationRecord
AutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detect
end
+ # Memoized per user: the layout renders the sidebar for desktop and mobile
+ # (each with three tab panels), so one request asks for the balance sheet
+ # many times; rebuilding it repeats the account/sync/exchange-rate queries.
def balance_sheet(user: Current.user)
- BalanceSheet.new(self, user: user)
+ @balance_sheets ||= {}
+ @balance_sheets[user&.id] ||= BalanceSheet.new(self, user: user)
end
def income_statement(user: Current.user, accounts: nil)