api_geo: cache requests for one day
This should make the 500 errors that we get in production less frequent. NB: Unfortunately we can't write a spec for the caching behavior, as the VCR mocks are never cached.
This commit is contained in:
parent
a0ae1afb45
commit
30774aa189
1 changed files with 9 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?
|
||||||
|
|
Loading…
Reference in a new issue