Change SafeRequestStore#write to accept an options hash

This change the write to accept an options hash to make
it compatible with ActiveSupport::Cache::Store#write
method.

The options hash are not passed to the underlying cache
implementation because RequestStore#write accepts only
a key, valye params.
parent cb8dc75b
...@@ -38,8 +38,8 @@ module Gitlab ...@@ -38,8 +38,8 @@ module Gitlab
value value
end end
def write(key, value, options = {}) def write(key, value, options = nil)
backend.write(cache_key(key), *[value.to_json, options].reject(&:blank?)) backend.write(cache_key(key), value.to_json, options)
end end
def fetch(key, options = {}, &block) def fetch(key, options = {}, &block)
......
...@@ -195,14 +195,14 @@ describe Gitlab::JsonCache do ...@@ -195,14 +195,14 @@ describe Gitlab::JsonCache do
it 'writes value to the cache with the given key' do it 'writes value to the cache with the given key' do
cache.write(key, true) cache.write(key, true)
expect(backend).to have_received(:write).with(expanded_key, "true") expect(backend).to have_received(:write).with(expanded_key, "true", nil)
end end
it 'writes a string containing a JSON representation of the value to the cache' do it 'writes a string containing a JSON representation of the value to the cache' do
cache.write(key, node) cache.write(key, node)
expect(backend).to have_received(:write) expect(backend).to have_received(:write)
.with(expanded_key, node.to_json) .with(expanded_key, node.to_json, nil)
end end
it 'passes options the underlying cache implementation' do it 'passes options the underlying cache implementation' do
...@@ -212,18 +212,18 @@ describe Gitlab::JsonCache do ...@@ -212,18 +212,18 @@ describe Gitlab::JsonCache do
.with(expanded_key, "true", expires_in: 15.seconds) .with(expanded_key, "true", expires_in: 15.seconds)
end end
it 'does not pass options to the underlying cache implementation when options is empty' do it 'passes options the underlying cache implementation when options is empty' do
cache.write(key, true, {}) cache.write(key, true, {})
expect(backend).to have_received(:write) expect(backend).to have_received(:write)
.with(expanded_key, "true") .with(expanded_key, "true", {})
end end
it 'does not pass options to the underlying cache implementation when options is nil' do it 'passes options the underlying cache implementation when options is nil' do
cache.write(key, true, nil) cache.write(key, true, nil)
expect(backend).to have_received(:write) expect(backend).to have_received(:write)
.with(expanded_key, "true") .with(expanded_key, "true", nil)
end end
end end
...@@ -234,6 +234,13 @@ describe Gitlab::JsonCache do ...@@ -234,6 +234,13 @@ describe Gitlab::JsonCache do
expect { cache.fetch(key) }.to raise_error(LocalJumpError) expect { cache.fetch(key) }.to raise_error(LocalJumpError)
end end
it 'passes options the underlying cache implementation' do
expect(backend).to receive(:write)
.with(expanded_key, "true", expires_in: 15.seconds)
cache.fetch(key, expires_in: 15.seconds) { true }
end
context 'when the given key does not exist in the cache' do context 'when the given key does not exist in the cache' do
context 'when the result of the block is truthy' do context 'when the result of the block is truthy' do
it 'returns the result of the block' do it 'returns the result of the block' do
...@@ -243,7 +250,7 @@ describe Gitlab::JsonCache do ...@@ -243,7 +250,7 @@ describe Gitlab::JsonCache do
end end
it 'caches the value' do it 'caches the value' do
expect(backend).to receive(:write).with(expanded_key, "true") expect(backend).to receive(:write).with(expanded_key, "true", {})
cache.fetch(key) { true } cache.fetch(key) { true }
end end
...@@ -257,7 +264,7 @@ describe Gitlab::JsonCache do ...@@ -257,7 +264,7 @@ describe Gitlab::JsonCache do
end end
it 'caches the value' do it 'caches the value' do
expect(backend).to receive(:write).with(expanded_key, "false") expect(backend).to receive(:write).with(expanded_key, "false", {})
cache.fetch(key) { false } cache.fetch(key) { false }
end end
...@@ -271,14 +278,14 @@ describe Gitlab::JsonCache do ...@@ -271,14 +278,14 @@ describe Gitlab::JsonCache do
end end
it 'caches the value' do it 'caches the value' do
expect(backend).to receive(:write).with(expanded_key, "null") expect(backend).to receive(:write).with(expanded_key, "null", {})
cache.fetch(key) { nil } cache.fetch(key) { nil }
end end
end end
end end
context 'whenn the given key exists in the cache' do context 'when the given key exists in the cache' do
context 'when the cached value is a hash' do context 'when the cached value is a hash' do
before do before do
backend.write(expanded_key, node.to_json) backend.write(expanded_key, node.to_json)
...@@ -384,7 +391,7 @@ describe Gitlab::JsonCache do ...@@ -384,7 +391,7 @@ describe Gitlab::JsonCache do
it 'writes the result of the block to the cache' do it 'writes the result of the block to the cache' do
expect(backend).to receive(:write) expect(backend).to receive(:write)
.with(expanded_key, 'block result'.to_json) .with(expanded_key, 'block result'.to_json, {})
cache.fetch(key) { 'block result' } cache.fetch(key) { 'block result' }
end end
......
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