discourse/lib/distributed_cache.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

30 lines
707 B
Ruby

# frozen_string_literal: true
require 'message_bus/distributed_cache'
class DistributedCache < MessageBus::DistributedCache
def initialize(key, manager: nil, namespace: true)
super(
key,
manager: manager,
namespace: namespace,
app_version: Discourse.git_version
)
end
# Defer setting of the key in the cache for performance critical path to avoid
# waiting on MessageBus to publish the message which involves writing to Redis.
def defer_set(k, v)
Scheduler::Defer.later("#{@key}_set") do
self[k] = v
end
end
def defer_get_set(k, &block)
return self[k] if hash.key? k
value = block.call
self.defer_set(k, value)
value
end
end