cacheizes all public stats

This commit is contained in:
Juanjo Bazán
2016-02-08 18:23:13 +01:00
committed by Juanjo Bazán
parent 07c009e0de
commit 2dafc74505

View File

@@ -6,17 +6,23 @@ class StatsController < ApplicationController
skip_authorization_check
def index
@visits = Visit.count
@debates = Debate.with_hidden.count
@proposals = Proposal.with_hidden.count
@comments = Comment.with_hidden.count
@visits = daily_cache('visits') { Visit.count }
@debates = daily_cache('debates') { Debate.with_hidden.count }
@proposals = daily_cache('proposals') { Proposal.with_hidden.count }
@comments = daily_cache('comments') { Comment.with_hidden.count }
@debate_votes = Vote.where(votable_type: 'Debate').count
@proposal_votes = Vote.where(votable_type: 'Proposal').count
@comment_votes = Vote.where(votable_type: 'Comment').count
@votes = Vote.count
@debate_votes = daily_cache('debate_votes') { Vote.where(votable_type: 'Debate').count }
@proposal_votes = daily_cache('proposal_votes') { Vote.where(votable_type: 'Proposal').count }
@comment_votes = daily_cache('comment_votes') { Vote.where(votable_type: 'Comment').count }
@votes = daily_cache('votes') { Vote.count }
@verified_users = User.with_hidden.level_two_or_three_verified.count
@unverified_users = User.with_hidden.unverified.count
@verified_users = daily_cache('verified_users') { User.with_hidden.level_two_or_three_verified.count }
@unverified_users = daily_cache('unverified_users') { User.with_hidden.unverified.count }
end
private
def daily_cache(key, &block)
Rails.cache.fetch("public_stats/#{Time.now.strftime("%Y-%m-%d")}/#{key}", &block)
end
end