Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Speed up checkout with caching of `countries_available_for_checkout` and `states_available_for_checkout`

Damian Legawiec88b8c933206a
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 184eb2f1bd..4e096cb385 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -197,11 +197,15 @@ module Spree
end
def countries_available_for_checkout
- @countries_available_for_checkout ||= checkout_zone.try(:country_list) || Spree::Country.all
+ @countries_available_for_checkout ||= Rails.cache.fetch(countries_available_for_checkout_cache_key) do
+ checkout_zone.try(:country_list) || Spree::Country.all
+ end
end
def states_available_for_checkout(country)
- checkout_zone.try(:state_list_for, country) || country.states
+ Rails.cache.fetch(states_available_for_checkout_cache_key(country)) do
+ checkout_zone.try(:state_list_for, country) || country.states
+ end
end
def checkout_zone_or_default
@@ -229,6 +233,14 @@ module Spree
private
+ def countries_available_for_checkout_cache_key
+ "#{cache_key_with_version}/#{checkout_zone&.cache_key_with_version}/countries_available_for_checkout"
+ end
+
+ def states_available_for_checkout_cache_key(country)
+ "#{cache_key_with_version}/#{checkout_zone&.cache_key_with_version}/states_available_for_checkout/#{country&.cache_key_with_version}"
+ end
+
def ensure_default_exists_and_is_unique
if default
Store.where.not(id: id).update_all(default: false)