Skip to content

Commit

Permalink
Merge pull request #64 from workarea-commerce/improvement-43-release-…
Browse files Browse the repository at this point in the history
…cache-keys

Improvement 43 release cache keys
  • Loading branch information
Ben Crouse committed Sep 17, 2019
2 parents 325ccd6 + d7b2b56 commit 281e7e8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
Expand Up @@ -6,7 +6,9 @@ class ApplicationGroup

class << self
def calculate(discounts, order, shippings)
groups = Rails.cache.fetch('discount_application_groups', expires_in: Workarea.config.cache_expirations.discount_application_groups, race_condition_ttl: 10) do
key = ['discount_application_groups', Release.current&.id].compact.join('/')
expiration = Workarea.config.cache_expirations.discount_application_groups
groups = Rails.cache.fetch(key, expires_in: expiration, race_condition_ttl: 10) do
# Ensure an undirected graph of compatibility on discounts
discounts.each do |discount|
discount.compatible_discount_ids.each do |id|
Expand Down
3 changes: 2 additions & 1 deletion core/app/models/workarea/pricing/discount/free_gift.rb
Expand Up @@ -125,8 +125,9 @@ def categories_qualify?(order)

def free_product_attributes(sku)
ttl = Workarea.config.cache_expirations.free_gift_attributes
key = ['workarea/free_gift', sku, Release.current&.id].compact.join('/')

Rails.cache.fetch("workarea/free_gift/#{sku}", expires_in: ttl) do
Rails.cache.fetch(key, expires_in: ttl) do
Workarea::OrderItemDetails.find(sku).try(:to_h) || {}
end
end
Expand Down
Expand Up @@ -11,13 +11,11 @@ def cache(*)
end

def cache_fragment_name(*)
result = super

if request.env['workarea.cache_varies'].present?
result << request.env['workarea.cache_varies']
end
super.tap { |result| result << cache_varies if cache_varies.present? }
end

result
def cache_varies
request.env['workarea.cache_varies']
end
end
end
Expand Down
Expand Up @@ -21,7 +21,7 @@ def render_content_blocks(blocks)
render_content_blocks_without_cache(blocks)
else
Rails.cache.fetch(
blocks.map(&:cache_key).join('/'),
blocks.map(&:cache_key).push(cache_varies).join('/'),
expires_in: Workarea.config.cache_expirations.render_content_blocks
) { render_content_blocks_without_cache(blocks) }
end
Expand Down
Expand Up @@ -11,6 +11,7 @@ def test_render_content_blocks
blocks = ContentBlockViewModel.wrap(content.blocks)

self.expects(:current_user).returns(nil)
self.expects(:request).returns(OpenStruct.new(env: {}))
Rails.cache.expects(:fetch).once
render_content_blocks(blocks)

Expand Down

0 comments on commit 281e7e8

Please sign in to comment.