option for writing to all nodes in a multicache ring
refs CNVS-30659 Change-Id: I2fcc74e9a4bede89adb1122bcea8fea7925805f2 Reviewed-on: https://gerrit.instructure.com/86735 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins Product-Review: Cody Cutrer <cody@instructure.com> QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
parent
a84970e47b
commit
bff69a3f00
|
@ -33,9 +33,26 @@ class MultiCache < ActiveSupport::Cache::Store
|
|||
|
||||
def fetch(key, options = nil, &block)
|
||||
options ||= {}
|
||||
# this makes the node "sticky" for read/write
|
||||
options[:node] = @ring[rand(@ring.length)]
|
||||
super(key, options, &block)
|
||||
# an option to allow populating all nodes in the ring with the
|
||||
# same data
|
||||
if options[:node] == :all
|
||||
calculated_value = nil
|
||||
did_calculate = false
|
||||
result = nil
|
||||
@ring.each do |node|
|
||||
options[:node] = node
|
||||
result = super(key, options) do
|
||||
calculated_value = yield unless did_calculate
|
||||
did_calculate = true
|
||||
calculated_value
|
||||
end
|
||||
end
|
||||
result
|
||||
else
|
||||
# this makes the node "sticky" for read/write
|
||||
options[:node] = @ring[rand(@ring.length)]
|
||||
super(key, options, &block)
|
||||
end
|
||||
end
|
||||
|
||||
# for compatibility
|
||||
|
|
|
@ -62,4 +62,17 @@ describe MultiCache do
|
|||
store = MultiCache.new(ring)
|
||||
expect(store.delete('key')).to eq true
|
||||
end
|
||||
|
||||
it 'allows writing to all nodes' do
|
||||
ring = [mock, mock]
|
||||
ring[0].expects(:get).once
|
||||
ring[0].expects(:set).once
|
||||
ring[1].expects(:get).once
|
||||
ring[1].expects(:set).once
|
||||
|
||||
store = MultiCache.new(ring)
|
||||
generated = 0
|
||||
store.fetch('key', node: :all) { generated += 1 }
|
||||
expect(generated).to eq 1
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue