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
Added more memoization to costly methods
Damian Legawiecb8ed034e996f
diff --git a/core/app/models/concerns/spree/memoized_data.rb b/core/app/models/concerns/spree/memoized_data.rbnew file mode 100644index 0000000000..4dfd927a65--- /dev/null+++ b/core/app/models/concerns/spree/memoized_data.rb@@ -0,0 +1,24 @@+module Spree+ module MemoizedData+ extend ActiveSupport::Concern++ included do+ # reset cache on save inside trasaction and transaction commit+ after_save :reset_memoized_data+ after_commit :reset_memoized_data++ def reload(options = {})+ reset_memoized_data+ super(options)+ end++ private++ def reset_memoized_data+ self.class.const_get('MEMOIZED_METHODS').each do |v|+ instance_variable_set(:"@#{v.gsub(/\?/, '')}", nil)+ end+ end+ end+ end+enddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex d3a170e98c..b76b543a12 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -20,6 +20,9 @@ module Spreeinclude NumberAsParaminclude SingleStoreResource+ include MemoizedData++ MEMOIZED_METHODS = %w(tax_zone)extend Spree::DisplayMoneymoney_methods :outstanding_balance, :item_total, :adjustment_total,@@ -428,7 +431,7 @@ module SpreeDEPRECATIONraise Spree.t(:cannot_empty_completed_order) if completed?-+result = Spree::Dependencies.cart_empty_service.constantize.call(order: self)result.valueend@@ -584,11 +587,6 @@ module Spreeupdate_column(:considered_risky, false)end- def reload(options = nil)- remove_instance_variable(:@tax_zone) if defined?(@tax_zone)- super- end-def tax_totalincluded_tax_total + additional_tax_totalenddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 6e554c60b0..115d64f458 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -23,6 +23,11 @@ module Spreeextend FriendlyIdinclude ProductScopesinclude MultiStoreResource+ include MemoizedData++ MEMOIZED_METHODS = %w(total_on_hand taxonomy_ids taxon_and_ancestors category+ default_variant_id tax_category default_variant+ purchasable? in_stock? backorderable?)friendly_id :slug_candidates, use: :history@@ -96,10 +101,6 @@ module Spreeafter_save :reset_nested_changesafter_touch :touch_taxons- # reset cache on save inside trasaction and transaction commit- after_save :reset_memoized_data- after_commit :reset_memoized_data-before_validation :downcase_slugbefore_validation :normalize_slug, on: :updatebefore_validation :validate_master@@ -139,13 +140,6 @@ module Spreealias master_images images- def reload- %w(total_on_hand taxonomy_ids taxon_and_ancestors category category default_variant_id tax_category default_variant).each do |v|- instance_variable_set(:"@#{v}", nil)- end- super- end-# Cant use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259def purchasable?variants_including_master.any?(&:purchasable?)@@ -180,8 +174,8 @@ module Spree# @return [Spree::Variant]def default_variant@default_variant ||= Rails.cache.fetch(default_variant_cache_key) do- if Spree::Config[:track_inventory_levels] && variants.in_stock_or_backorderable.any?- variants.in_stock_or_backorderable.first+ if Spree::Config[:track_inventory_levels] && available_variant = variants.detect(&:purchasable?)+ available_variantelsehas_variants? ? variants.first : masterend@@ -488,12 +482,6 @@ module Spreeendend- def reset_memoized_data- %w(total_on_hand taxonomy_ids taxon_and_ancestors category default_variant_id tax_category default_variant).each do |v|- instance_variable_set(:"@#{v}", nil)- end- end-def requires_price?Spree::Config[:require_master_price]enddiff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rbindex d73571e7ec..aa337562da 100644--- a/core/app/models/spree/stock/quantifier.rb+++ b/core/app/models/spree/stock/quantifier.rb@@ -9,15 +9,15 @@ module Spreeenddef total_on_hand- if variant.should_track_inventory?- stock_items.sum(:count_on_hand)- else- Float::INFINITY- end+ @total_on_hand ||= if variant.should_track_inventory?+ stock_items.sum(:count_on_hand)+ else+ Float::INFINITY+ endenddef backorderable?- stock_items.any?(&:backorderable)+ @backorderable ||= stock_items.any?(&:backorderable)enddef can_supply?(required = 1)diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 6314513849..dcdb8d8fc5 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -3,6 +3,10 @@ module Spreeacts_as_paranoidacts_as_list scope: :product+ include MemoizedData++ MEMOIZED_METHODS = %w(purchasable in_stock backorderable tax_category options_text compare_at_price)+belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variantsbelongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true@@ -247,7 +251,7 @@ module Spree# Check if model responds to cache version and fall back to updated_at for older rails versions# This makes sure a version is supplied when recyclable cache keys are disabled.version = respond_to?(:cache_version) ? cache_version : updated_at.to_i- Rails.cache.fetch(in_stock_cache_key, version: version) do+ @in_stock ||= Rails.cache.fetch(in_stock_cache_key, version: version) dototal_on_hand > 0endend@@ -257,7 +261,7 @@ module Spreealias is_backorderable? backorderable?def purchasable?- in_stock? || backorderable?+ @purchasable ||= in_stock? || backorderable?end# Shortcut method to determine if inventory tracking is enabled for this variant@@ -287,7 +291,7 @@ module Spreeenddef backordered?- total_on_hand <= 0 && stock_items.exists?(backorderable: true)+ @backordered ||= total_on_hand <= 0 && stock_items.exists?(backorderable: true)endprivate