Class: MessageBus::DistributedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/message_bus/distributed_cache.rb

Defined Under Namespace

Classes: Manager

Constant Summary collapse

DEFAULT_SITE_ID =
'default'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, manager: nil, namespace: true, app_version: nil) ⇒ DistributedCache

Returns a new instance of DistributedCache.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/message_bus/distributed_cache.rb', line 112

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

#keyObject (readonly)

Returns the value of attribute key.



110
111
112
# File 'lib/message_bus/distributed_cache.rb', line 110

def key
  @key
end

Class Method Details

.default_managerObject



106
107
108
# File 'lib/message_bus/distributed_cache.rb', line 106

def self.default_manager
  @default_manager
end

Instance Method Details

#[](k) ⇒ Object



135
136
137
138
# File 'lib/message_bus/distributed_cache.rb', line 135

def [](k)
  k = k.to_s if Symbol === k
  hash[k]
end

#[]=(k, v) ⇒ Object



129
130
131
132
133
# File 'lib/message_bus/distributed_cache.rb', line 129

def []=(k, v)
  k = k.to_s if Symbol === k
  @manager.set(self, k, v)
  hash[k] = v
end

#clearObject



146
147
148
149
# File 'lib/message_bus/distributed_cache.rb', line 146

def clear
  @manager.clear(self)
  hash.clear
end

#delete(k, publish: true) ⇒ Object



140
141
142
143
144
# File 'lib/message_bus/distributed_cache.rb', line 140

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



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/message_bus/distributed_cache.rb', line 151

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

#identityObject



124
125
126
127
# File 'lib/message_bus/distributed_cache.rb', line 124

def identity
  # fork resilient / multi machine identity
  (@seed_id ||= SecureRandom.hex) + "#{Process.pid}"
end