Merge pull request #3129 from betagouv/cache-geo-api-requests
Cache Geo Api requests
This commit is contained in:
commit
eeea623cf9
3 changed files with 70 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
||||||
class ApiGeo::API
|
class ApiGeo::API
|
||||||
TIMEOUT = 15
|
TIMEOUT = 15
|
||||||
|
CACHE_DURATION = 1.day
|
||||||
|
|
||||||
def self.regions
|
def self.regions
|
||||||
url = [API_GEO_URL, "regions"].join("/")
|
url = [API_GEO_URL, "regions"].join("/")
|
||||||
|
@ -27,6 +28,11 @@ class ApiGeo::API
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.call(url, body, method = :get)
|
def self.call(url, body, method = :get)
|
||||||
|
# The cache engine is stored, because as of Typhoeus 1.3.1 the cache engine instance
|
||||||
|
# is included in the computed `cache_key`.
|
||||||
|
# (Which means that when the cache instance changes, the cache is invalidated.)
|
||||||
|
@typhoeus_cache ||= Typhoeus::Cache::SuccessfulRequestsRailsCache.new
|
||||||
|
|
||||||
response = Typhoeus::Request.new(
|
response = Typhoeus::Request.new(
|
||||||
url,
|
url,
|
||||||
method: method,
|
method: method,
|
||||||
|
@ -37,7 +43,9 @@ class ApiGeo::API
|
||||||
headers: {
|
headers: {
|
||||||
'Accept' => 'application/json',
|
'Accept' => 'application/json',
|
||||||
'Accept-Encoding' => 'gzip, deflate'
|
'Accept-Encoding' => 'gzip, deflate'
|
||||||
}.merge(method == :post ? { 'Content-Type' => 'application/json' } : {})
|
}.merge(method == :post ? { 'Content-Type' => 'application/json' } : {}),
|
||||||
|
cache: @typhoeus_cache,
|
||||||
|
cache_ttl: CACHE_DURATION
|
||||||
).run
|
).run
|
||||||
|
|
||||||
if response.success?
|
if response.success?
|
||||||
|
|
21
app/lib/typhoeus/cache/successful_requests_rails_cache.rb
vendored
Normal file
21
app/lib/typhoeus/cache/successful_requests_rails_cache.rb
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
module Typhoeus
|
||||||
|
module Cache
|
||||||
|
# Cache successful Typhoeus requests in the Rails cache
|
||||||
|
# (but don’t cache failed requests).
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# Typhoeus.config.cache = Typhoeus::Cache::SuccessfulRequestsRailsCache.new
|
||||||
|
# Typhoeus.get('http://exemple.com/api', cache_ttl: 1.day)
|
||||||
|
class SuccessfulRequestsRailsCache
|
||||||
|
def get(request)
|
||||||
|
::Rails.cache.read(request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(request, response)
|
||||||
|
if response&.success?
|
||||||
|
::Rails.cache.write(request, response, expires_in: request.cache_ttl)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
40
spec/lib/typhoeus/cache/successful_requests_rails_cache.rb
vendored
Normal file
40
spec/lib/typhoeus/cache/successful_requests_rails_cache.rb
vendored
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Typhoeus::Cache::SuccessfulRequestsRailsCache, lib: true do
|
||||||
|
let(:cache) { described_class.new }
|
||||||
|
|
||||||
|
let(:base_url) { "localhost:3001" }
|
||||||
|
let(:request) { Typhoeus::Request.new(base_url, { :method => :get, cache: cache, cache_ttl: 1.day }) }
|
||||||
|
let(:response) { Typhoeus::Response.new(:response_code => response_code, :return_code => 0, :mock => true) }
|
||||||
|
let(:response_code) { 0 }
|
||||||
|
|
||||||
|
before { Rails.cache.clear }
|
||||||
|
|
||||||
|
describe "#set" do
|
||||||
|
context 'when the request is successful' do
|
||||||
|
let(:response_code) { 200 }
|
||||||
|
|
||||||
|
it 'saves the request in the Rails cache' do
|
||||||
|
cache.set(request, response)
|
||||||
|
expect(Rails.cache.exist?(request)).to be true
|
||||||
|
expect(Rails.cache.read(request)).to be_a(Typhoeus::Response)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the request failed' do
|
||||||
|
let(:response_code) { 500 }
|
||||||
|
|
||||||
|
it 'doesn’t save the request in the Rails cache' do
|
||||||
|
cache.set(request, response)
|
||||||
|
expect(Rails.cache.exist?(request)).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#get" do
|
||||||
|
it 'returns the request in the cache' do
|
||||||
|
Rails.cache.write(request, response)
|
||||||
|
expect(cache.get(request)).to be_a(Typhoeus::Response)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue