Extract caching of homepage stats to service

This commit is contained in:
Matt-Yorkley
2020-04-22 14:55:59 +02:00
parent 56b9737676
commit 545ca85644
2 changed files with 5 additions and 2 deletions

View File

@@ -18,9 +18,11 @@ class HomeController < BaseController
private
# Cache the value of the query count for 24 hours
# Cache the value of the query count
def cached_count(key, query)
Rails.cache.fetch("home_stats_count_#{key}", expires_in: 1.day, race_condition_ttl: 10) do
CacheService.cache("home_stats_count_#{key}",
expires_in: CacheService::HOME_STATS_EXPIRY,
race_condition_ttl: 10) do
query.count
end
end

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
class CacheService
HOME_STATS_EXPIRY = 1.day.freeze
FILTERS_EXPIRY = 30.seconds.freeze
def self.cache(cache_key, options = {})