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.rbindex 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::SyncStatusMonitorattr_reader :familydef syncing_account_ids- Rails.cache.fetch(cache_key) do+ @syncing_account_ids ||= Rails.cache.fetch(cache_key) doSync.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.rbindex c2ae27dca7..97aa264d56 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -330,8 +330,12 @@ class Family < ApplicationRecordAutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detectend+ # 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)enddef income_statement(user: Current.user, accounts: nil)