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.
This commit is contained in:
Javi Martín
2019-04-10 15:07:51 +02:00
parent 7c0e499eee
commit aa0e813970
3 changed files with 8 additions and 11 deletions

View File

@@ -107,19 +107,19 @@ class Budget::Stats
end end
def authors def authors
budget.investments.pluck(:author_id) @authors ||= budget.investments.pluck(:author_id)
end end
def voters def voters
supports(budget).distinct.pluck(:voter_id) @voters ||= supports(budget).distinct.pluck(:voter_id)
end end
def balloters 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 end
def poll_ballot_voters def poll_ballot_voters
budget&.poll ? budget.poll.voters.pluck(:user_id) : [] @poll_ballot_voters ||= budget.poll ? budget.poll.voters.pluck(:user_id) : []
end end
def balloters_by_heading(heading_id) def balloters_by_heading(heading_id)
@@ -174,8 +174,6 @@ class Budget::Stats
end end
stats_cache(*stats_methods) stats_cache(*stats_methods)
stats_cache :total_participants_with_gender
stats_cache :voters, :participants, :authors, :balloters, :poll_ballot_voters
def stats_cache(key, &block) def stats_cache(key, &block)
Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block) Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block)

View File

@@ -34,7 +34,7 @@ module Statisticable
end end
def participants def participants
User.unscoped.where(id: participant_ids) @participants ||= User.unscoped.where(id: participant_ids)
end end
def total_male_participants def total_male_participants
@@ -103,7 +103,7 @@ module Statisticable
end end
def total_participants_with_gender def total_participants_with_gender
participants.where.not(gender: nil).distinct.count @total_participants_with_gender ||= participants.where.not(gender: nil).distinct.count
end end
def age_groups def age_groups

View File

@@ -96,15 +96,14 @@ class Poll::Stats
end end
def voters def voters
poll.voters @voters ||= poll.voters
end end
def recounts def recounts
poll.recounts @recounts ||= poll.recounts
end end
stats_cache(*stats_methods) stats_cache(*stats_methods)
stats_cache :participants, :voters, :recounts
def stats_cache(key, &block) def stats_cache(key, &block)
Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &block) Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &block)