diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index e167d5462c..afde45d3bd 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -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 diff --git a/app/services/cache_service.rb b/app/services/cache_service.rb index 682911457e..dec5214e1a 100644 --- a/app/services/cache_service.rb +++ b/app/services/cache_service.rb @@ -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.