2017-08-02 04:11:48 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-16 03:01:25 +08:00
|
|
|
require 'message_bus/distributed_cache'
|
2014-11-12 06:43:41 +08:00
|
|
|
|
2018-10-16 03:01:25 +08:00
|
|
|
class DistributedCache < MessageBus::DistributedCache
|
2017-10-20 16:39:31 +08:00
|
|
|
def initialize(key, manager: nil, namespace: true)
|
2018-10-16 03:01:25 +08:00
|
|
|
super(
|
|
|
|
key,
|
|
|
|
manager: manager,
|
|
|
|
namespace: namespace,
|
|
|
|
app_version: Discourse.git_version
|
|
|
|
)
|
2014-11-12 06:43:41 +08:00
|
|
|
end
|
2021-06-02 15:46:48 +08:00
|
|
|
|
|
|
|
# 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
|
2021-06-03 14:02:40 +08:00
|
|
|
|
|
|
|
def defer_get_set(k, &block)
|
2022-02-18 16:51:14 +08:00
|
|
|
return self[k] if hash.key? k
|
2021-06-03 14:02:40 +08:00
|
|
|
value = block.call
|
|
|
|
self.defer_set(k, value)
|
|
|
|
value
|
|
|
|
end
|
2014-11-12 06:43:41 +08:00
|
|
|
end
|