From aa0e813970178a85740a133e3414f3d9a183aa26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 10 Apr 2019 15:07:51 +0200 Subject: [PATCH] Use ruby cache for stats helper methods These methods are only used while stats are being generated; once stats are generated, they aren't used anymore. So there's no need to store them using the Dalli cache. Furthermore, there are polls (and even budgets) with hundreds of thousands of participants. Calculating stats for them takes a very long time because we can't store all those records in the Dalli cache. However, since these records aren't used once the stats are generated, we can store them in an instance variable while we generate the stats, speeding up the process. --- app/models/budget/stats.rb | 10 ++++------ app/models/concerns/statisticable.rb | 4 ++-- app/models/poll/stats.rb | 5 ++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index 766e6ef77..cd9bff741 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -107,19 +107,19 @@ class Budget::Stats end def authors - budget.investments.pluck(:author_id) + @authors ||= budget.investments.pluck(:author_id) end def voters - supports(budget).distinct.pluck(:voter_id) + @voters ||= supports(budget).distinct.pluck(:voter_id) end def balloters - budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact + @balloters ||= budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact end def poll_ballot_voters - budget&.poll ? budget.poll.voters.pluck(:user_id) : [] + @poll_ballot_voters ||= budget.poll ? budget.poll.voters.pluck(:user_id) : [] end def balloters_by_heading(heading_id) @@ -174,8 +174,6 @@ class Budget::Stats end stats_cache(*stats_methods) - stats_cache :total_participants_with_gender - stats_cache :voters, :participants, :authors, :balloters, :poll_ballot_voters def stats_cache(key, &block) Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block) diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 14cb1fa9d..0839f32fa 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -34,7 +34,7 @@ module Statisticable end def participants - User.unscoped.where(id: participant_ids) + @participants ||= User.unscoped.where(id: participant_ids) end def total_male_participants @@ -103,7 +103,7 @@ module Statisticable end def total_participants_with_gender - participants.where.not(gender: nil).distinct.count + @total_participants_with_gender ||= participants.where.not(gender: nil).distinct.count end def age_groups diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index a6be63640..ea075e6c9 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -96,15 +96,14 @@ class Poll::Stats end def voters - poll.voters + @voters ||= poll.voters end def recounts - poll.recounts + @recounts ||= poll.recounts end stats_cache(*stats_methods) - stats_cache :participants, :voters, :recounts def stats_cache(key, &block) Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &block)