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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add memomization to some methods to speed things up
Damian Legawiecb2794e2469fe
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 8921d211ee..86a6c04295 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -160,7 +160,7 @@ module Spree## @return [Spree::Variant]def default_variant- Rails.cache.fetch(default_variant_cache_key) do+ @default_variant ||= Rails.cache.fetch(default_variant_cache_key) doif Spree::Config[:track_inventory_levels] && variants.in_stock_or_backorderable.any?variants.in_stock_or_backorderable.firstelse@@ -172,11 +172,11 @@ module Spree# Returns default Variant ID for Product# @return [Integer]def default_variant_id- default_variant.id+ @default_variant_id ||= default_variant.idenddef tax_category- super || TaxCategory.find_by(is_default: true)+ @tax_category ||= super || TaxCategory.find_by(is_default: true)end# Adding properties and option types on creation based on a chosen prototype@@ -288,11 +288,11 @@ module Spreeenddef total_on_hand- if any_variants_not_track_inventory?- Float::INFINITY- else- stock_items.sum(:count_on_hand)- end+ @total_on_hand ||= if any_variants_not_track_inventory?+ Float::INFINITY+ else+ stock_items.sum(:count_on_hand)+ endend# Master variant may be deleted (i.e. when the product is deleted)@@ -303,14 +303,11 @@ module Spreeenddef brand- taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_brands_name) })+ @brand ||= taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_brands_name) })enddef category- taxons.joins(:taxonomy).- where(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) }).- order(depth: :desc).- first+ @category ||= taxons.joins(:taxonomy).order(depth: :desc).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) })endprivate@@ -440,12 +437,12 @@ module Spreeenddef taxon_and_ancestors- taxons.map(&:self_and_ancestors).flatten.uniq+ @taxon_and_ancestors ||= taxons.map(&:self_and_ancestors).flatten.uniqend# Get the taxonomy ids of all taxons assigned to this product and their ancestors.def taxonomy_ids- taxon_and_ancestors.map(&:taxonomy_id).flatten.uniq+ @taxonomy_ids ||= taxon_and_ancestors.map(&:taxonomy_id).flatten.uniqend# Iterate through this products taxons and taxonomies and touch their timestamps in a batchdiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 2c8848a0bf..ce5005b073 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -123,15 +123,15 @@ module Spreeenddef tax_category- if self[:tax_category_id].nil?- product.tax_category- else- Spree::TaxCategory.find(self[:tax_category_id])- end+ @tax_category ||= if self[:tax_category_id].nil?+ product.tax_category+ else+ Spree::TaxCategory.find(self[:tax_category_id])+ endenddef options_text- Spree::Variants::OptionsPresenter.new(self).to_sentence+ @options_text ||= Spree::Variants::OptionsPresenter.new(self).to_sentenceend# Default to master name@@ -226,7 +226,7 @@ module Spreeenddef compare_at_price- price_in(cost_currency).try(:compare_at_amount)+ @compare_at_price ||= price_in(cost_currency).try(:compare_at_amount)enddef name_and_skudiff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rbindex 1049929305..f8ce4bc0d0 100644--- a/frontend/app/helpers/spree/frontend_helper.rb+++ b/frontend/app/helpers/spree/frontend_helper.rb@@ -295,7 +295,7 @@ module Spreeenddef checkout_available_payment_methods- @order.available_payment_methods(current_store)+ @checkout_available_payment_methods ||= @order.available_payment_methods(current_store)endprivatediff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rbindex 9127d07548..e584f9fa5c 100644--- a/frontend/app/helpers/spree/navigation_helper.rb+++ b/frontend/app/helpers/spree/navigation_helper.rb@@ -3,7 +3,7 @@ require 'digest'module Spreemodule NavigationHelperdef spree_navigation_data- SpreeStorefrontConfig.dig(current_store.code, :navigation) || SpreeStorefrontConfig.dig(:default, :navigation) || []+ @spree_navigation_data ||= SpreeStorefrontConfig.dig(current_store.code, :navigation) || SpreeStorefrontConfig.dig(:default, :navigation) || []# safeguard for older Spree installs that don't have spree_navigation initializer# or spree.yml file presentrescue