-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use cache aside pattern * Configure TTL to 24 hours * Add tests
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
modules/claims_api/lib/bgs_service/redis/find_poas_response.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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ClaimsApi | ||
class FindPOAsResponse | ||
attr_reader :response | ||
|
||
def initialize(response) | ||
@response = response | ||
end | ||
|
||
def cache? | ||
@response.is_a?(Array) && @response.size.positive? | ||
end | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
modules/claims_api/lib/bgs_service/redis/find_poas_service.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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'common/models/redis_store' | ||
require 'common/models/concerns/cache_aside' | ||
require 'bgs_service/standard_data_web_service' | ||
require 'bgs_service/redis/find_poas_response' | ||
|
||
module ClaimsApi | ||
class FindPOAsService < ::Common::RedisStore | ||
include ::Common::CacheAside | ||
|
||
redis_config_key :bgs_find_poas_response | ||
|
||
def response | ||
@response ||= response_from_redis_or_service.response | ||
end | ||
|
||
private | ||
|
||
def todays_date | ||
Time.zone.now.to_date.to_s | ||
end | ||
|
||
def response_from_redis_or_service | ||
do_cached_with(key: todays_date) do | ||
response = standard_data_web_service.find_poas | ||
|
||
ClaimsApi::FindPOAsResponse.new(filter_response(response)) | ||
end | ||
end | ||
|
||
def filter_response(response) | ||
response.map { |poa| poa.slice(:legacy_poa_cd, :ptcpnt_id) } | ||
end | ||
|
||
def standard_data_web_service | ||
@service ||= ClaimsApi::StandardDataWebService.new(external_uid: 'find_poas_service_uid', | ||
external_key: 'find_poas_service_key') | ||
end | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
modules/claims_api/spec/lib/claims_api/bgs/redis/find_poas_service_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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'bgs_service/redis/find_poas_service' | ||
|
||
describe ClaimsApi::FindPOAsService do | ||
describe '#response' do | ||
context 'when the response is not cached' do | ||
it 'calls bgs and returns the response' do | ||
VCR.use_cassette('claims_api/bgs/standard_data_web_service/find_poas') do | ||
service = described_class.new | ||
allow(service).to receive(:standard_data_web_service).and_call_original | ||
response = service.response | ||
|
||
expect(service).to have_received(:standard_data_web_service) | ||
expect(response).to be_an_instance_of(Array) | ||
expect(response.first).to include(:legacy_poa_cd, :ptcpnt_id) | ||
end | ||
end | ||
end | ||
|
||
context 'when the response is cached' do | ||
it 'does not call bgs and returns the cached response' do | ||
VCR.use_cassette('claims_api/bgs/standard_data_web_service/find_poas') do | ||
# Call BGS and cache the response | ||
first_service = described_class.new | ||
first_service.response | ||
|
||
# The second service should not call BGS | ||
second_service = described_class.new | ||
allow(second_service).to receive(:standard_data_web_service).and_call_original | ||
second_service.response | ||
|
||
expect(second_service).not_to have_received(:standard_data_web_service) | ||
expect(second_service.response).to be_an_instance_of(Array) | ||
expect(second_service.response.first).to include(:legacy_poa_cd, :ptcpnt_id) | ||
end | ||
end | ||
end | ||
end | ||
end |