mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 19:03:13 +08:00
b7404373cf
Instead of having to remember every time, just always wait until the current transaction (if it exists) has committed before clearing any DistributedCache. The only exception to this is caches that aren't caching things from postgres. This means we have to do the test setup after setting the test transaction, because doing the test setup involves clearing caches. Reapplying this - it now doesn't use after_commit if skip_db is set
31 lines
815 B
Ruby
31 lines
815 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") { self[k] = v }
|
|
end
|
|
|
|
def defer_get_set(k, &block)
|
|
return self[k] if hash.key? k
|
|
value = block.call
|
|
self.defer_set(k, value)
|
|
value
|
|
end
|
|
|
|
def clear(after_commit: true)
|
|
if after_commit && !GlobalSetting.skip_db?
|
|
DB.after_commit { super() }
|
|
else
|
|
super()
|
|
end
|
|
end
|
|
end
|