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 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.rb
new file mode 100644
index 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
+end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index d3a170e98c..b76b543a12 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -20,6 +20,9 @@ module Spree
include NumberAsParam
include SingleStoreResource
+ include MemoizedData
+
+ MEMOIZED_METHODS = %w(tax_zone)
extend Spree::DisplayMoney
money_methods :outstanding_balance, :item_total, :adjustment_total,
@@ -428,7 +431,7 @@ module Spree
DEPRECATION
raise Spree.t(:cannot_empty_completed_order) if completed?
-
+
result = Spree::Dependencies.cart_empty_service.constantize.call(order: self)
result.value
end
@@ -584,11 +587,6 @@ module Spree
update_column(:considered_risky, false)
end
- def reload(options = nil)
- remove_instance_variable(:@tax_zone) if defined?(@tax_zone)
- super
- end
-
def tax_total
included_tax_total + additional_tax_total
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 6e554c60b0..115d64f458 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -23,6 +23,11 @@ module Spree
extend FriendlyId
include ProductScopes
include 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 Spree
after_save :reset_nested_changes
after_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_slug
before_validation :normalize_slug, on: :update
before_validation :validate_master
@@ -139,13 +140,6 @@ module Spree
alias 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/259
def 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_variant
else
has_variants? ? variants.first : master
end
@@ -488,12 +482,6 @@ module Spree
end
end
- 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]
end
diff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rb
index d73571e7ec..aa337562da 100644
--- a/core/app/models/spree/stock/quantifier.rb
+++ b/core/app/models/spree/stock/quantifier.rb
@@ -9,15 +9,15 @@ module Spree
end
def 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
+ end
end
def backorderable?
- stock_items.any?(&:backorderable)
+ @backorderable ||= stock_items.any?(&:backorderable)
end
def can_supply?(required = 1)
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 6314513849..dcdb8d8fc5 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -3,6 +3,10 @@ module Spree
acts_as_paranoid
acts_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: :variants
belongs_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) do
total_on_hand > 0
end
end
@@ -257,7 +261,7 @@ module Spree
alias 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 Spree
end
def backordered?
- total_on_hand <= 0 && stock_items.exists?(backorderable: true)
+ @backordered ||= total_on_hand <= 0 && stock_items.exists?(backorderable: true)
end
private