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

Caching on heavily-used API calls: srch/api/profiles #4763

Merged
merged 2 commits into from
Feb 5, 2019
Merged
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ gem 'execjs' # See https://github.com/sstephenson/execjs#readme for more support
gem 'sass-rails', '~> 5.0', '>= 5.0.7'
gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
gem 'grape-rails-cache'

group :development do
gem 'byebug', platform: :mri
Expand Down
28 changes: 16 additions & 12 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Search < Grape::API
# see /app/api/srch/shared_params.rb
helpers SharedParams

include Grape::Rails::Cache

# Endpoint definitions
# Basic implementation from classic plots2 SearchController
resource :srch do
Expand Down Expand Up @@ -92,19 +94,21 @@ class Search < Grape::API
end
get :profiles do
search_request = SearchRequest.fromRequest(params)
results = Search.execute(:profiles, params)

if results.present?
docs = results.map do |model|
DocResult.new(
doc_type: 'USERS',
doc_url: '/profile/' + model.name,
doc_title: model.username
)
cache(key: "api:profiles:#{params[:query]}:#{params[:limit]}:#{params[:sort_by]}:#{params[:order_direction]}:#{params[:field]}", expires_in: 2.day) do
results = Search.execute(:profiles, params)

if results.present?
docs = results.map do |model|
DocResult.new(
doc_type: 'USERS',
doc_url: '/profile/' + model.name,
doc_title: model.username
)
end
DocList.new(docs, search_request)
else
DocList.new('', search_request)
end
DocList.new(docs, search_request)
else
DocList.new('', search_request)
end
end

Expand Down