Skip to content

Commit

Permalink
feat(badges): use simple in-memory cache for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 17, 2017
1 parent 83540e5 commit 2453c55
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def forbidden?
private

def to_svg
badges_service.pact_verification_badge pact, label, initials, verification_status
badge_service.pact_verification_badge pact, label, initials, verification_status
end

def pact
Expand Down
27 changes: 27 additions & 0 deletions lib/pact_broker/badges/cached_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pact_broker/logging'
require 'pact_broker/badges/service'

module PactBroker
module Badges
module CachedService

extend self
include PactBroker::Logging
extend PactBroker::Services

CACHE = {}
private_constant :CACHE

def pact_verification_badge pact, label, initials, verification_status
badge_key = key(pact, label, initials, verification_status)
CACHE[badge_key] ||= PactBroker::Badges::Service.pact_verification_badge(pact, label, initials, verification_status)
end

private

def key pact, label, initials, verification_status
"#{pact.consumer.name}-#{pact.provider.name}-#{label}-#{initials}-#{verification_status}"
end
end
end
end
6 changes: 3 additions & 3 deletions lib/pact_broker/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def verification_service
Verifications::Service
end

def badges_service
require 'pact_broker/badges/service'
Badges::Service
def badge_service
require 'pact_broker/badges/cached_service'
Badges::CachedService
end
end
end
49 changes: 49 additions & 0 deletions spec/lib/pact_broker/badges/cached_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'pact_broker/badges/cached_service'
require 'pact_broker/badges/service'

module PactBroker
module Badges
describe CachedService do

let(:consumer) { double('consumer', name: 'foo') }
let(:provider) { double('provider', name: 'bar') }
let(:pact) { double('pact', consumer: consumer, provider: provider) }
let(:label) { 'consumer' }
let(:initials) { false }
let(:verification_status) { 'status' }

describe "#pact_verification_badge" do

before do
allow(Service).to receive(:pact_verification_badge).and_return('badge')
stub_const('PactBroker::Badges::CachedService::CACHE', {})
end

subject { CachedService.pact_verification_badge pact, label, initials, verification_status }

it "returns the badge" do
expect(subject).to eq 'badge'
end

context "when the badge is not in the cache" do
before do
stub_const('PactBroker::Badges::CachedService::CACHE', {})
end

it "retrieves the badge from the Badges::Service" do
expect(Service).to receive(:pact_verification_badge)
subject
end
end

context "when the badge is in the cache" do
it "returns the cached badge" do
expect(Service).to receive(:pact_verification_badge).once
CachedService.pact_verification_badge pact, label, initials, verification_status
CachedService.pact_verification_badge pact, label, initials, verification_status
end
end
end
end
end
end

0 comments on commit 2453c55

Please sign in to comment.