class RubySkynet::Zookeeper::CachedRegistry

Public Class Methods

new(params) click to toggle source

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

Calls superclass method
# 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

Public Instance Methods

[](key) click to toggle source

Retrieve the latest value from a specific key from the registry

# File lib/ruby_skynet/zookeeper/cached_registry.rb, line 49
def [](key)
  @cache[key]
end
each_pair(&block) click to toggle source

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

Example:

registry.each_pair {|k,v| puts "#{k} => #{v}"}
# 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
keys() click to toggle source

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

# File lib/ruby_skynet/zookeeper/cached_registry.rb, line 64
def keys
  @cache.keys
end
to_h() click to toggle source

Returns a copy of the registry as a Hash

# File lib/ruby_skynet/zookeeper/cached_registry.rb, line 69
def to_h
  @cache.dup
end