Skip to content

Commit

Permalink
caching questions stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cesswairimu committed Dec 16, 2018
1 parent 8b89a32 commit be99757
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ def filtering(period)
return if period.nil?

if period == 'All'
@asked = Node.questions.to_a.size
@answered = Answer.all.map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered"
Rails.cache.fetch("all_stats", expires_in: 1.days) do
@asked = Node.questions.to_a.size
@answered = Answer.all.map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered"
end
else
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).to_a.size
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered in the past #{period}"
Rails.cache.fetch("#{period}_stats", expires_in: 1.days) do
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).to_a.size
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered in the past #{period}"
end
end
end

Expand Down

0 comments on commit be99757

Please sign in to comment.