Commit 7c9da21e authored by Dylan Griffith's avatar Dylan Griffith

Cache /search/count requests in the browser

These requests are typically loaded by the same user repeatedly as they
switch tabs on the search page. Considering that Elasticsearch indexing
is only updated every 1-2 minutes anyway the 1 minute browser side cache
shouldn't really be noticeable to users except for loading counts
faster.

This is also using a `private` cache (default for `expires_in`) so that
it is not cached by intermediate proxies. This is important since each
user's permissions is considered in a search.
parent d7560d31
......@@ -49,6 +49,12 @@ class SearchController < ApplicationController
scope = search_service.scope
count = search_service.search_results.formatted_count(scope)
# Users switching tabs will keep fetching the same tab counts so it's a
# good idea to cache in their browser just for a short time. They can still
# clear cache if they are seeing an incorrect count but inaccurate count is
# not such a bad thing.
expires_in 1.minute
render json: { count: count }
end
......
---
title: Cache /search/count requests in the browser
merge_request: 55036
author:
type: performance
......@@ -252,6 +252,14 @@ RSpec.describe SearchController do
get :count, params: { search: 'hello' }
end.to raise_error(ActionController::ParameterMissing)
end
it 'sets private cache control headers' do
get :count, params: { search: 'hello', scope: 'projects' }
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Cache-Control']).to include('max-age=60, private')
end
end
describe 'GET #autocomplete' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment