Commit 555bb5a0 authored by Robert May's avatar Robert May

Fix for nested cache calls

parent 68b7a7f2
...@@ -81,7 +81,13 @@ module API ...@@ -81,7 +81,13 @@ module API
# @return [Gitlab::Json::PrecompiledJson] # @return [Gitlab::Json::PrecompiledJson]
def cache_action(key, **cache_opts) def cache_action(key, **cache_opts)
json = cache.fetch(key, **apply_default_cache_options(cache_opts)) do json = cache.fetch(key, **apply_default_cache_options(cache_opts)) do
Gitlab::Json.dump(yield.as_json) response = yield
if response.is_a?(Gitlab::Json::PrecompiledJson)
response.to_s
else
Gitlab::Json.dump(response.as_json)
end
end end
body Gitlab::Json::PrecompiledJson.new(json) body Gitlab::Json::PrecompiledJson.new(json)
......
...@@ -177,6 +177,16 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do ...@@ -177,6 +177,16 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do
5.times { perform } 5.times { perform }
end end
it "handles nested cache calls" do
nested_call = instance.cache_action(cache_key, **kwargs) do
instance.cache_action([:nested], **kwargs) do
expensive_thing.do_very_expensive_action
end
end
expect(nested_call.to_s).to eq(subject.to_s)
end
end end
describe "#cache_action_if" do describe "#cache_action_if" do
......
...@@ -20,7 +20,7 @@ RSpec.describe API::Branches do ...@@ -20,7 +20,7 @@ RSpec.describe API::Branches do
stub_feature_flags(branch_list_keyset_pagination: false) stub_feature_flags(branch_list_keyset_pagination: false)
end end
describe "GET /projects/:id/repository/branches" do describe "GET /projects/:id/repository/branches", :use_clean_rails_redis_caching do
let(:route) { "/projects/#{project_id}/repository/branches" } let(:route) { "/projects/#{project_id}/repository/branches" }
shared_examples_for 'repository branches' do shared_examples_for 'repository branches' 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