Use Etag caching for pipelines json

Enable caching in the Etag::Middleware and when a pipeline changes
status, expire the cache for the project pipelines path.
This commit is contained in:
Toon Claes 2017-04-04 17:38:16 +02:00 committed by Kamil Trzcinski
parent ebae10467d
commit 847b9c8232
No known key found for this signature in database
GPG Key ID: 4505F5C7E12C6A5A
5 changed files with 50 additions and 0 deletions

View File

@ -88,6 +88,8 @@ module Ci
pipeline.run_after_commit do
PipelineHooksWorker.perform_async(id)
Ci::ExpirePipelineCacheService.new(project, nil)
.execute(pipeline)
end
end

View File

@ -0,0 +1,18 @@
module Ci
class ExpirePipelineCacheService < BaseService
def execute(pipeline)
@pipeline = pipeline
Gitlab::EtagCaching::Store.new.touch(project_pipelines_path)
end
private
def project_pipelines_path
Gitlab::Routing.url_helpers.namespace_project_pipelines_path(
project.namespace,
project,
format: :json)
end
end
end

View File

@ -10,6 +10,10 @@ module Gitlab
{
regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/issues/\d+/rendered_title\z),
name: 'issue_title'
},
{
regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/pipelines\.json\z),
name: 'project_pipelines'
}
].freeze

View File

@ -335,6 +335,14 @@ describe Ci::Pipeline, models: true do
end
end
describe 'pipeline ETag caching' do
it 'executes ExpirePipelinesCacheService' do
expect_any_instance_of(Ci::ExpirePipelineCacheService).to receive(:execute).with(pipeline)
pipeline.cancel
end
end
def create_build(name, queued_at = current, started_from = 0)
create(:ci_build,
name: name,

View File

@ -0,0 +1,18 @@
require 'spec_helper'
describe Ci::ExpirePipelineCacheService, services: true do
let(:user) { create(:user) }
let(:project) { create(:empty_project) }
let(:pipeline) { create(:ci_pipeline, project: project) }
subject { described_class.new(project, user) }
describe '#execute' do
it 'invalidate Etag caching for project pipelines path' do
path = "/#{project.full_path}/pipelines.json"
expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(path)
subject.execute(pipeline)
end
end
end