Class: DistributedCache
- Inherits:
-
MessageBus::DistributedCache
- Object
- MessageBus::DistributedCache
- DistributedCache
- Defined in:
- lib/distributed_cache.rb
Instance Method Summary collapse
- #clear(after_commit: true) ⇒ Object
- #clear_regex(regex) ⇒ Object
- #defer_get_set(k, &block) ⇒ Object
- #defer_get_set_bulk(ks, key_blk, &blk) ⇒ Object
-
#defer_set(k, v) ⇒ Object
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.
-
#initialize(key, manager: nil, namespace: true) ⇒ DistributedCache
constructor
A new instance of DistributedCache.
Constructor Details
#initialize(key, manager: nil, namespace: true) ⇒ DistributedCache
Returns a new instance of DistributedCache.
6 7 8 |
# File 'lib/distributed_cache.rb', line 6 def initialize(key, manager: nil, namespace: true) super(key, manager: manager, namespace: namespace, app_version: Discourse.git_version) end |
Instance Method Details
#clear(after_commit: true) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/distributed_cache.rb', line 43 def clear(after_commit: true) if after_commit && !GlobalSetting.skip_db? DB.after_commit { super() } else super() end end |
#clear_regex(regex) ⇒ Object
51 52 53 |
# File 'lib/distributed_cache.rb', line 51 def clear_regex(regex) hash.keys.select { |k| k =~ regex }.each { |k| delete(k) } end |
#defer_get_set(k, &block) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/distributed_cache.rb', line 16 def defer_get_set(k, &block) raise TypeError if !Rails.env.production? && !k.is_a?(String) return self[k] if hash.key? k value = block.call self.defer_set(k, value) value end |
#defer_get_set_bulk(ks, key_blk, &blk) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/distributed_cache.rb', line 25 def defer_get_set_bulk(ks, key_blk, &blk) found_keys, missing_keys = ks.partition { |k| hash.key?(key_blk.call(k)) } found_hash = found_keys.map { |key| [key, self[key_blk.call(key)]] }.to_h if missing_keys.present? missing_values = blk.call(missing_keys.freeze) missing_hash = missing_keys.zip(missing_values).to_h Scheduler::Defer.later("#{@key}_bulk_set") do missing_hash.each { |key, value| self[key_blk.call(key)] = value } end ks.zip(missing_hash.merge(found_hash).values_at(*ks)).to_h else found_hash end end |
#defer_set(k, v) ⇒ Object
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.
12 13 14 |
# File 'lib/distributed_cache.rb', line 12 def defer_set(k, v) Scheduler::Defer.later("#{@key}_set") { self[k] = v } end |