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.

Added caching of `Variant#price_in` (#11599)

Damian Legawiecf24e08184f87
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 5ea20f98f7..21b887226f 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -209,7 +209,20 @@ module Spree
end
def price_in(currency)
- prices.detect { |price| price.currency == currency&.upcase } || prices.build(currency: currency&.upcase)
+ currency = currency&.upcase
+ find_or_build_price = lambda do
+ if prices.loaded?
+ prices.detect { |price| price.currency == currency } || prices.build(currency: currency)
+ else
+ prices.find_or_initialize_by(currency: currency)
+ end
+ end
+
+ Rails.cache.fetch("spree/prices/#{cache_key_with_version}/price_in/#{currency}") do
+ find_or_build_price.call
+ end
+ rescue TypeError
+ find_or_build_price.call
end
def amount_in(currency)