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

@@ -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)