Cache default country for an hour

The default country usually never changes after the initial setup of an
instance.
This commit is contained in:
Maikel Linke
2022-11-02 15:33:43 +11:00
parent 257959448a
commit f971131888
2 changed files with 7 additions and 1 deletions

View File

@@ -6,6 +6,12 @@ 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

View File

@@ -10,6 +10,6 @@ class DefaultCountry
end
def self.country
Spree::Country.find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first
Spree::Country.cached_find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first
end
end