Class: RubySkynet::Zookeeper::CachedRegistry

Inherits:
Registry
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/ruby_skynet/zookeeper/cached_registry.rb

Instance Attribute Summary

Attributes inherited from Registry

#root

Instance Method Summary collapse

Methods inherited from Registry

#[]=, #close, #delete, #on_create, #on_delete, #on_update

Constructor Details

#initialize(params) ⇒ CachedRegistry

Create a CachedRegistry instance to manage information within the Registry and keep a local cached copy of the data in the Registry to support high-speed or frequent reads.

Writes are sent to ZooKeeper and then replicated back to the local cache only once ZooKeeper has updated its store

See RubySkynet::Zookeeper::Registry for the complete list of options



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_skynet/zookeeper/cached_registry.rb', line 36

def initialize(params)
  @cache = ThreadSafe::Hash.new
  # Supplied block to load the current keys from the Registry
  super(params) do |key, value, version|
    @cache[key] = value
  end

  on_create {|key, value|          @cache[key] = value}
  on_update {|key, value, version| @cache[key] = value}
  on_delete {|key|                 @cache.delete(key)}
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the latest value from a specific key from the registry



49
50
51
# File 'lib/ruby_skynet/zookeeper/cached_registry.rb', line 49

def [](key)
  @cache[key]
end

#each_pair(&block) ⇒ Object

Iterate over every key, value pair in the registry at the root_path

Example:

registry.each_pair {|k,v| puts "#{k} => #{v}"}


57
58
59
60
61
# File 'lib/ruby_skynet/zookeeper/cached_registry.rb', line 57

def each_pair(&block)
  # Have to duplicate the cache otherwise concurrent changes to the
  # registry will interfere with the iterator
  @cache.dup.each_pair(&block)
end

#keysObject

Returns [Array<String>] all keys in the registry



64
65
66
# File 'lib/ruby_skynet/zookeeper/cached_registry.rb', line 64

def keys
  @cache.keys
end

#to_hObject

Returns a copy of the registry as a Hash



69
70
71
# File 'lib/ruby_skynet/zookeeper/cached_registry.rb', line 69

def to_h
  @cache.dup
end