Extract home stats caching to method in service

This commit is contained in:
Matt-Yorkley
2020-04-30 10:15:46 +02:00
parent 2292cbaae4
commit 37821beb1b
2 changed files with 10 additions and 4 deletions

View File

@@ -19,10 +19,8 @@ class HomeController < BaseController
private
# Cache the value of the query count
def cached_count(key, query)
CacheService.cache("home_stats_count_#{key}",
expires_in: CacheService::HOME_STATS_EXPIRY,
race_condition_ttl: 10) do
def cached_count(statistic, query)
CacheService.home_stats(statistic) do
query.count
end
end

View File

@@ -25,6 +25,14 @@ class CacheService
cached_class.maximum(:updated_at).to_i
end
def self.home_stats(statistic)
Rails.cache.fetch("home_stats_count_#{statistic}",
expires_in: HOME_STATS_EXPIRY,
race_condition_ttl: 10) do
yield
end
end
module FragmentCaching
# Rails' caching in views is called "Fragment Caching" and uses some slightly different logic.
# Note: supplied keys are actually prepended with "view/" under the hood.