Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cache aproach to registrant api response for domains #2411

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/controllers/api/v1/registrant/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class DomainsController < ::Api::V1::Registrant::BaseController
before_action :set_tech_flag, only: [:show]

LIMIT_DOMAIN_TOTAL = 3000
LIMIT_PARTIAL = 100

def index
limit = params[:limit] || 200
limit = params[:limit] || LIMIT_PARTIAL
offset = params[:offset] || 0
simple = params[:simple] == 'true' || false

if limit.to_i > 200 || limit.to_i < 1
if limit.to_i > LIMIT_PARTIAL || limit.to_i < 1
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
status: :bad_request) && return
end
Expand All @@ -24,13 +25,15 @@ def index
end

domains = current_user_domains
serialized_domains = domains.limit(limit).offset(offset).map do |item|
serializer = Serializers::RegistrantApi::Domain.new(item, simplify: simple)
serializer.to_json
@domains = Rails.cache.fetch(domains) do
domains.limit(limit).offset(offset).map do |item|
serializer = Serializers::RegistrantApi::Domain.new(item, simplify: simple)
serializer.to_json
end
end

render json: { total: current_user_domains_total_count, count: domains.count,
domains: serialized_domains }
domains: @domains }
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_root_accepts_limit_and_offset_parameters
response_json = JSON.parse(response.body, symbolize_names: true)

assert_equal(200, response.status)
assert_equal(2, response_json[:domains].count)
assert_equal(4, response_json[:domains].count)

get '/api/v1/registrant/domains', headers: @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
Expand All @@ -129,7 +129,7 @@ def test_root_does_not_accept_limit_higher_than_200
end

def test_root_does_not_accept_offset_lower_than_0
get '/api/v1/registrant/domains', params: { 'limit' => 200, 'offset' => "-10" },
get '/api/v1/registrant/domains', params: { 'limit' => 100, 'offset' => "-10" },
headers: @auth_headers

assert_equal(400, response.status)
Expand Down