Class: RubyDoozer::CachedRegistry

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

Instance Attribute Summary

Attributes inherited from Registry

#current_revision, #doozer_config, #doozer_pool, #root

Instance Method Summary collapse

Methods inherited from Registry

#[]=, #delete, #finalize, #on_delete, #on_update

Constructor Details

#initialize(params) ⇒ CachedRegistry

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

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

See RubyDoozer::Registry for complete list of options



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_doozer/cached_registry.rb', line 35

def initialize(params)
  super
  @cache = ThreadSafe::Hash.new

  key = "#{@root}/**"
  doozer_pool.with_connection do |doozer|
    @current_revision = doozer.current_revision
    # Fetch all the configuration information from Doozer and set the internal copy
    doozer.walk(key, @current_revision) do |key, value, revision|
      set_cached_value(relative_key(key), @deserializer.deserialize(value))
    end
  end

  # Start monitoring thread
  monitor_thread
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the latest value from a specific key from the registry



53
54
55
# File 'lib/ruby_doozer/cached_registry.rb', line 53

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}"}


61
62
63
64
65
# File 'lib/ruby_doozer/cached_registry.rb', line 61

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

#keysObject

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



68
69
70
# File 'lib/ruby_doozer/cached_registry.rb', line 68

def keys
  @cache.keys
end

#on_create(key = '*', &block) ⇒ Object

When an entry is created the block will be called

Parameters
  key
    The relative key to watch for changes
  block
    The block to be called

Parameters passed to the block:
  key
    The key that was created
    Supplying a key of '*' means all paths
    Default: '*'

  value
    New value from doozer

Example:

registry.on_update do |key, value, revision|
  puts "#{key} was created with #{value}"
end


97
98
99
# File 'lib/ruby_doozer/cached_registry.rb', line 97

def on_create(key='*', &block)
  ((@create_subscribers ||= ThreadSafe::Hash.new)[key] ||= ThreadSafe::Array.new) << block
end

#to_hObject

Returns a copy of the registry as a Hash



73
74
75
# File 'lib/ruby_doozer/cached_registry.rb', line 73

def to_h
  @cache.dup
end