From f9711318889b767377a8cc0df795732d80c23f53 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 2 Nov 2022 15:33:43 +1100 Subject: [PATCH] Cache default country for an hour The default country usually never changes after the initial setup of an instance. --- app/models/spree/country.rb | 6 ++++++ app/services/default_country.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/spree/country.rb b/app/models/spree/country.rb index ee94968556..6647b8d698 100644 --- a/app/models/spree/country.rb +++ b/app/models/spree/country.rb @@ -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 diff --git a/app/services/default_country.rb b/app/services/default_country.rb index 11b85e90a4..cb2aae92ec 100644 --- a/app/services/default_country.rb +++ b/app/services/default_country.rb @@ -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