Skip to content

Commit

Permalink
Add find POAs caching service
Browse files Browse the repository at this point in the history
* Use cache aside pattern
* Configure TTL to 24 hours
* Add tests
  • Loading branch information
tycol7 committed Sep 7, 2024
1 parent 4d2ca74 commit 3c15be8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ development: &defaults
brd_response_store:
namespace: brd_response_store
each_ttl: 82800
bgs_find_poas_response:
namespace: bgs_find_poas_response
each_ttl: 86400
test:
<<: *defaults
redis:
Expand Down
15 changes: 15 additions & 0 deletions modules/claims_api/lib/bgs_service/redis/find_poas_response.rb
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 modules/claims_api/lib/bgs_service/redis/find_poas_service.rb
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
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

0 comments on commit 3c15be8

Please sign in to comment.