Class: MessageBus::DistributedCache
- Inherits:
-
Object
- Object
- MessageBus::DistributedCache
- Defined in:
- lib/message_bus/distributed_cache.rb
Overview
Like a hash, just does its best to stay in sync across the farm. On boot all instances are blank, but they populate as various processes fill it up.
Defined Under Namespace
Classes: Manager
Constant Summary collapse
- DEFAULT_SITE_ID =
'default'
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #clear ⇒ Object
- #delete(k, publish: true) ⇒ Object
- #hash(site_id_arg = nil) ⇒ Object
- #identity ⇒ Object
-
#initialize(key, manager: nil, namespace: true, app_version: nil) ⇒ DistributedCache
constructor
A new instance of DistributedCache.
Constructor Details
#initialize(key, manager: nil, namespace: true, app_version: nil) ⇒ DistributedCache
Returns a new instance of DistributedCache.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/message_bus/distributed_cache.rb', line 116 def initialize(key, manager: nil, namespace: true, app_version: nil) @key = key @data = {} @manager = manager || DistributedCache.default_manager @manager.app_version = app_version if app_version @namespace = namespace @app_version = app_version @manager.ensure_subscribe! @manager.register(self) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
114 115 116 |
# File 'lib/message_bus/distributed_cache.rb', line 114 def key @key end |
Class Method Details
.default_manager ⇒ Object
110 111 112 |
# File 'lib/message_bus/distributed_cache.rb', line 110 def self.default_manager @default_manager end |
Instance Method Details
#[](k) ⇒ Object
139 140 141 142 |
# File 'lib/message_bus/distributed_cache.rb', line 139 def [](k) k = k.to_s if Symbol === k hash[k] end |
#[]=(k, v) ⇒ Object
133 134 135 136 137 |
# File 'lib/message_bus/distributed_cache.rb', line 133 def []=(k, v) k = k.to_s if Symbol === k @manager.set(self, k, v) hash[k] = v end |
#clear ⇒ Object
150 151 152 153 |
# File 'lib/message_bus/distributed_cache.rb', line 150 def clear @manager.clear(self) hash.clear end |
#delete(k, publish: true) ⇒ Object
144 145 146 147 148 |
# File 'lib/message_bus/distributed_cache.rb', line 144 def delete(k, publish: true) k = k.to_s if Symbol === k @manager.delete(self, k) if publish hash.delete(k) end |
#hash(site_id_arg = nil) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/message_bus/distributed_cache.rb', line 155 def hash(site_id_arg = nil) site_id = if @namespace site_id_arg || (MessageBus.site_id_lookup && MessageBus.site_id_lookup.call) || DEFAULT_SITE_ID else DEFAULT_SITE_ID end @data[site_id] ||= {} end |
#identity ⇒ Object
128 129 130 131 |
# File 'lib/message_bus/distributed_cache.rb', line 128 def identity # fork resilient / multi machine identity (@seed_id ||= SecureRandom.hex) + "#{Process.pid}" end |