-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #2058 invalidate cache for frequently use API
- Loading branch information
Showing
11 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
app/controllers/concerns/spree_cm_commissioner/content_cachable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module SpreeCmCommissioner | ||
module ContentCachable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_action :set_cache_control_for_cdn | ||
end | ||
|
||
def set_cache_control_for_cdn | ||
return unless request.get? || request.head? | ||
return unless request.base_url == ENV['CONTENT_HOST_URL'] | ||
|
||
response.set_header('Cache-Control', 'public, max-age=7200') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/interactors/spree_cm_commissioner/invalidate_cache_request.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'aws-sdk-cloudfront' | ||
|
||
# pattern: '/api/v2/storefront/homepage_sections*' | ||
# https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/invalidation-access-logs.html | ||
|
||
module SpreeCmCommissioner | ||
class InvalidateCacheRequest < BaseInteractor | ||
delegate :pattern, to: :context | ||
|
||
def call | ||
client = ::Aws::CloudFront::Client.new( | ||
region: ENV.fetch('AWS_REGION'), | ||
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), | ||
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), | ||
http_open_timeout: 15, | ||
http_read_timeout: 60 | ||
) | ||
|
||
context.response = client.create_invalidation( | ||
distribution_id: ENV.fetch('ASSETS_SYNC_CF_DIST_ID'), | ||
invalidation_batch: { | ||
caller_reference: Time.now.to_i.to_s, | ||
paths: { | ||
quantity: 1, | ||
items: [pattern] | ||
} | ||
} | ||
) | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
app/jobs/spree_cm_commissioner/invalidate_cache_request_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module SpreeCmCommissioner | ||
class InvalidateCacheRequestJob < ApplicationJob | ||
def perform(pattern) | ||
SpreeCmCommissioner::InvalidateCacheRequest.call(pattern: pattern) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...mmissioner_InvalidateCacheRequest/_call/requested_to_invalidate_cache_return_response.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
spec/interactors/spree_cm_commissioner/invalidate_cache_request_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SpreeCmCommissioner::InvalidateCacheRequest do | ||
before do | ||
ENV['AWS_REGION'] = 'ap-southeast-1' | ||
ENV['AWS_ACCESS_KEY_ID'] = 'AXXXXXXXXXXXXXXXY' | ||
ENV['AWS_SECRET_ACCESS_KEY'] = 'VO103FAKEFAKEFAKE3020X=I230x1' | ||
ENV['ASSETS_SYNC_CF_DIST_ID'] = 'D12FAKEFAKE' | ||
end | ||
|
||
describe '.call', :vcr do | ||
it 'requested to invalidate cache & return response' do | ||
context = described_class.call(pattern: '/staging/422.html') | ||
|
||
expect(context.response.location).to eq "https://cloudfront.amazonaws.com/2020-05-31/distribution/D12FAKEFAKE/invalidation/I2PGO1F2LAWEKXXYAKMN0P38N1" | ||
expect(context.response.invalidation.id).to eq "I2PGO1F2LAWEKXXYAKMN0P38N1" | ||
expect(context.response.invalidation.status).to eq "InProgress" | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
spec/jobs/spree_cm_commissioner/invalidate_cache_request_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SpreeCmCommissioner::InvalidateCacheRequestJob do | ||
describe '#perform' do | ||
it 'invokes StateUpdater.call' do | ||
expect(SpreeCmCommissioner::InvalidateCacheRequest).to receive(:call).with(pattern: '/api/storefront/*') | ||
described_class.new.perform('/api/storefront/*') | ||
end | ||
end | ||
end |