Skip to content

Commit

Permalink
Added caching for failed / empty rates to prevent retries.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDQ committed May 14, 2010
1 parent fd267b2 commit e47ec45
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/calculator/active_shipping.rb
Expand Up @@ -28,7 +28,7 @@ def compute(line_items)
rates = retrieve_rates(origin, destination, packages(order))
end

return nil unless rates
return nil if rates.empty?
rate = rates[self.description].to_f + (Spree::ActiveShipping::Config[:handling_fee].to_f || 0.0)
return nil unless rate
# divide by 100 since active_shipping rates are expressed as cents
Expand All @@ -51,6 +51,8 @@ def retrieve_rates(origin, destination, packages)
message = re.message
end

Rails.cache.write @cache_key, {} #write empty hash to cache to prevent constant re-lookups

raise Spree::ShippingError.new("#{I18n.t('shipping_error')}: #{message}")
end
end
Expand All @@ -68,6 +70,6 @@ def packages(order)
def cache_key(line_items)
order = line_items.first.order
addr = order.ship_address
"#{carrier.name}-#{order.number}-#{addr.country.iso}-#{addr.state ? addr.state.abbr : addr.state_name}-#{addr.city}-#{addr.zipcode}-#{line_items.map {|li| li.variant_id.to_s + "_" + li.quantity.to_s }.join("|")}"
@cache_key = "#{carrier.name}-#{order.number}-#{addr.country.iso}-#{addr.state ? addr.state.abbr : addr.state_name}-#{addr.city}-#{addr.zipcode}-#{line_items.map {|li| li.variant_id.to_s + "_" + li.quantity.to_s }.join("|")}"
end
end

0 comments on commit e47ec45

Please sign in to comment.