Class: LaunchDarkly::MemoizedValue

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/memoized_value.rb

Overview

Simple implementation of a thread-safe memoized value whose generator function will never be run more than once, and whose value can be overridden by explicit assignment. Note that we no longer use this class and it will be removed in a future version.

Instance Method Summary collapse

Constructor Details

#initialize(&generator) ⇒ MemoizedValue

Returns a new instance of MemoizedValue.



8
9
10
11
12
13
# File 'lib/ldclient-rb/memoized_value.rb', line 8

def initialize(&generator)
  @generator = generator
  @mutex = Mutex.new
  @inited = false
  @value = nil
end

Instance Method Details

#getObject



15
16
17
18
19
20
21
22
23
# File 'lib/ldclient-rb/memoized_value.rb', line 15

def get
  @mutex.synchronize do
    unless @inited
      @value = @generator.call
      @inited = true
    end
  end
  @value
end

#set(value) ⇒ Object



25
26
27
28
29
30
# File 'lib/ldclient-rb/memoized_value.rb', line 25

def set(value)
  @mutex.synchronize do
    @value = value
    @inited = true
  end
end