From cb3cd3e7eaa631e0bd0cc9d93417fe01e0340e0e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 4 Jan 2024 13:45:36 +1100 Subject: [PATCH] Cache default country simply in memory This simplifies the code, avoids a method naming collision with Rails and should be faster as well. --- app/models/spree/country.rb | 6 ------ app/services/default_country.rb | 7 +++++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/models/spree/country.rb b/app/models/spree/country.rb index cc9e3dbed9..ca83337caf 100644 --- a/app/models/spree/country.rb +++ b/app/models/spree/country.rb @@ -6,12 +6,6 @@ module Spree validates :name, :iso_name, presence: true - def self.cached_find_by(attrs) - Rails.cache.fetch("countries/#{attrs.hash}", expires_in: 1.hour) do - find_by(attrs) - end - end - def <=>(other) name <=> other.name end diff --git a/app/services/default_country.rb b/app/services/default_country.rb index 609535a1fc..0e07d5c68c 100644 --- a/app/services/default_country.rb +++ b/app/services/default_country.rb @@ -10,7 +10,10 @@ class DefaultCountry end def self.country - Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", - nil)) || Spree::Country.first + # Changing ENV requires restarting the process. + iso = ENV.fetch("DEFAULT_COUNTRY_CODE", nil) + + # When ENV changes on restart, this cache will be reset as well. + @country ||= Spree::Country.find_by(iso:) || Spree::Country.first end end