mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Extract home stats caching to method in service
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user