Skip to content
代码片段 群组 项目
提交 ef36ad7e 编辑于 作者: Dmitry Gruzd's avatar Dmitry Gruzd 提交者: John Mason
浏览文件

Set Cache-Control for autocomplete_sources

上级 1df70dd0
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
module AutocompleteSources
module ExpiresIn
extend ActiveSupport::Concern
AUTOCOMPLETE_EXPIRES_IN = 3.minutes
AUTOCOMPLETE_CACHED_ACTIONS = [:members, :commands, :labels].freeze
included do
before_action :set_expires_in, only: AUTOCOMPLETE_CACHED_ACTIONS
end
private
def set_expires_in
case action_name.to_sym
when :members
expires_in AUTOCOMPLETE_EXPIRES_IN if Feature.enabled?(:cache_autocomplete_sources_members, current_user)
when :commands
expires_in AUTOCOMPLETE_EXPIRES_IN if Feature.enabled?(:cache_autocomplete_sources_commands, current_user)
when :labels
expires_in AUTOCOMPLETE_EXPIRES_IN if Feature.enabled?(:cache_autocomplete_sources_labels, current_user)
end
end
end
end
# frozen_string_literal: true
class Groups::AutocompleteSourcesController < Groups::ApplicationController
include AutocompleteSources::ExpiresIn
feature_category :groups_and_projects, [:members]
feature_category :team_planning, [:issues, :labels, :milestones, :commands]
feature_category :code_review_workflow, [:merge_requests]
......@@ -8,11 +10,6 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
urgency :low, [:issues, :labels, :milestones, :commands, :merge_requests, :members]
def members
if Feature.enabled?(:cache_autocomplete_sources_members, current_user)
# Cache the response on the frontend
expires_in 3.minutes
end
render json: ::Groups::ParticipantsService.new(@group, current_user).execute(target)
end
......
# frozen_string_literal: true
class Projects::AutocompleteSourcesController < Projects::ApplicationController
include AutocompleteSources::ExpiresIn
before_action :authorize_read_milestone!, only: :milestones
before_action :authorize_read_crm_contact!, only: :contacts
......@@ -13,11 +15,6 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController
urgency :low, [:issues, :labels, :milestones, :commands, :contacts]
def members
if Feature.enabled?(:cache_autocomplete_sources_members, current_user)
# Cache the response on the frontend
expires_in 3.minutes
end
render json: ::Projects::ParticipantsService.new(@project, current_user).execute(target)
end
......
---
name: cache_autocomplete_sources_commands
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/138226
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/433168
milestone: '16.7'
type: development
group: group::global search
default_enabled: false
---
name: cache_autocomplete_sources_labels
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/138226
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/433170
milestone: '16.7'
type: development
group: group::global search
default_enabled: false
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AutocompleteSources::ExpiresIn, feature_category: :global_search do
controller(ActionController::Base) do
include AutocompleteSources::ExpiresIn
def members
render json: []
end
def commands
render json: []
end
def labels
render json: []
end
def not_cached
render json: []
end
end
before do
routes.draw do
get "members" => "anonymous#members"
get "commands" => "anonymous#commands"
get "labels" => "anonymous#labels"
get "not_cached" => "anonymous#not_cached"
end
end
let(:expected_cache_control) { "max-age=#{described_class::AUTOCOMPLETE_EXPIRES_IN}, private" }
described_class::AUTOCOMPLETE_CACHED_ACTIONS.each do |action|
context "when action is #{action} with feature flag enabled" do
it "sets correct cache-control" do
get action
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Cache-Control']).to eq(expected_cache_control)
end
end
context "when action is #{action} with feature flag disabled" do
before do
stub_feature_flags("cache_autocomplete_sources_#{action}" => false)
end
it 'does not set cache-control' do
get action
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Cache-Control']).to be_nil
end
end
end
context 'when action is not in AUTOCOMPLETE_CACHED_ACTIONS' do
it 'does not set cache-control' do
get :not_cached
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Cache-Control']).to be_nil
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册