From 37821beb1be4aa0de95f30082f04fb310b78e23e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 30 Apr 2020 10:15:46 +0200 Subject: [PATCH] Extract home stats caching to method in service --- app/controllers/home_controller.rb | 6 ++---- app/services/cache_service.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) 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.