Skip to content

Commit

Permalink
add cache_ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
west2538 committed Jul 8, 2019
1 parent e073cfb commit 7f12327
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/controllers/posts_controller.rb
Expand Up @@ -13,7 +13,7 @@ def index

@online_users = User.where.not(online_at: nil).limit(12).order(online_at: :desc)

@special_posts = Post.includes(:comments).where.not(scenario_start: nil).where('scenario_start <= ?', Date.today).where('scenario_end >= ?', Date.today)
@special_posts = cache_specialposts
if @special_posts.present?
array_posts01 = @posts01.to_a
@special_posts = @special_posts.to_a
Expand All @@ -22,7 +22,7 @@ def index
end
end

@ranks = Comment.where('created_at > ?', Time.now - 7.days).group(:post_id).order(Arel.sql('count(post_id) desc')).limit(3).pluck(:post_id)
@ranks = cache_ranks
@boukensha_count = User.count
@clears_count = Comment.count
@user_clears_count = Comment.where(user_uid: @current_user.uid).count
Expand Down Expand Up @@ -351,4 +351,18 @@ def cache_maps
end
end

private
def cache_specialposts
Rails.cache.fetch("cache_specialposts", expires_in: 1.hour) do
Post.includes(:comments).where.not(scenario_start: nil).where('scenario_start <= ?', Date.today).where('scenario_end >= ?', Date.today).to_a
end
end

private
def cache_ranks
Rails.cache.fetch("cache_ranks", expires_in: 1.hour) do
Comment.where('created_at > ?', Time.now - 7.days).group(:post_id).order(Arel.sql('count(post_id) desc')).limit(3).pluck(:post_id).to_a
end
end

end

0 comments on commit 7f12327

Please sign in to comment.