discourse/spec/lib/distributed_cache_spec.rb
David Taylor d7b48d0229
FIX: Handle nil values in DistributedCache#defer_get_set (stable) (#15980)
Themes often cache `nil` values in a DistributedCache. This bug meant that we were re-calculating some values on every request, AND triggering message-bus publishing on every request.

This fix should provide a significant performance improvement for busy sites.
2022-02-18 08:51:14 +00:00

32 lines
767 B
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe "DistributedCache extensions" do
let(:cache) { DistributedCache.new('mytest') }
it "can defer_get_set" do
messages = MessageBus.track_publish("/distributed_hash") do
cache.defer_get_set("key") { "value" }
end
expect(messages.size).to eq(1)
expect(cache["key"]).to eq("value")
end
it "works correctly for nil values" do
block_called_counter = 0
messages = MessageBus.track_publish("/distributed_hash") do
2.times do
cache.defer_get_set("key") do
block_called_counter += 1
nil
end
end
end
expect(block_called_counter).to eq(1)
expect(messages.size).to eq(1)
expect(cache["key"]).to eq(nil)
end
end