Added cache to the common data request

pull/5304/head
Mario de Frutos 9 years ago
parent c28db1700b
commit 0a6a9b8034

@ -0,0 +1,53 @@
# encoding: utf-8
class CommonDataRedisCache
# This needs to be changed whenever there're changes in the code that require invalidation of old keys
VERSION = '1'
def initialize(redis_cache = $tables_metadata)
@redis = redis_cache
end
def get(is_https_request)
key = key(is_https_request)
value = redis.get(key)
if value.present?
return JSON.parse(value, symbolize_names: true)
else
return nil
end
rescue Redis::BaseError => exception
CartoDB.notify_exception(exception, { key: key })
nil
end
def set(is_https_request, response_headers, response_body)
serialized = JSON.generate({headers: response_headers,
body: response_body
})
redis.setex(key(is_https_request), 6.hours.to_i, serialized)
rescue Redis::BaseError => exception
CartoDB.notify_exception(exception, { key: key, headers: response_headers, body: response_body })
nil
end
def invalidate
redis.del [key(is_https_request=true), key(is_https_request=false)]
rescue Redis::BaseError => exception
CartoDB.notify_exception(exception)
nil
end
def key(is_https_request=false)
protocol = is_https_request ? 'https' : 'http'
"common_data:request:#{protocol}:#{VERSION}"
end
private
def redis
@redis
end
end

@ -1,5 +1,6 @@
require_relative '../../lib/carto/http/client'
require_relative '../../services/sql-api/sql_api'
require_relative '../helpers/common_data_redis_cache'
class CommonData
@ -73,15 +74,21 @@ class CommonData
body = nil
begin
http_client = Carto::Http::Client.get('common_data', log_requests: true)
response = http_client.request(
request = http_client.request(
@visualizations_api_url,
method: :get,
connecttimeout: CONNECT_TIMEOUT,
timeout: DEFAULT_TIMEOUT,
params: {per_page: NO_PAGE_LIMIT}
).run
)
is_https_request = (request.url =~ /^https:\/\//)
cached_data = redis_cache.get(is_https_request)
return cached_data[:body] unless cached_data.nil?
response = request.run
if response.code == 200
body = response.response_body
redis_cache.set(is_https_request, response.headers, response.response_body)
body
end
rescue Exception => e
CartoDB.notify_exception(e)
@ -111,4 +118,8 @@ class CommonData
default
end
end
def redis_cache
@redis_cache ||= CommonDataRedisCache.new
end
end

@ -11,6 +11,7 @@ describe CommonData do
@common_data.stubs(:config).with('base_url').returns(nil)
@common_data.stubs(:config).with('api_key').returns('wadus')
@common_data.stubs(:config).with('format', 'shp').returns('shp')
CommonDataRedisCache.new.invalidate
end
after(:all) do

Loading…
Cancel
Save