Class: Daemon::Utils::ProcessCache

Inherits:
Object
  • Object
show all
Defined in:
lib/onapp/engine/utils/process_cache.rb

Instance Method Summary (collapse)

Constructor Details

- (ProcessCache) initialize(accessor, destructor = proc{})

Returns a new instance of ProcessCache



6
7
8
9
10
11
12
13
14
15
# File 'lib/onapp/engine/utils/process_cache.rb', line 6

def initialize(accessor, destructor = proc{})
  unless accessor.respond_to?(:call) && destructor.respond_to?(:call)
    raise ArgumentError, 'Require block here'
  end

  @accessor = accessor
  @destructor = destructor
  @pid = nil
  extend(::MonitorMixin)
end

Instance Method Details

- (Object) access



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/onapp/engine/utils/process_cache.rb', line 17

def access
  synchronize do
    if @pid && @pid != ::Process.pid
      @destructor.call
    end

    if @pid == ::Process.pid
      @cache
    else
      @pid = ::Process.pid
      @cache = @accessor.call
    end
  end
end