class Google::Cloud::Env::LazyDict
outside of Google::Cloud::Env.
it to be available to other libraries. Currently it should not be used
We keep this private for now so we can move it in the future if we need
process of computation concurrently and independently.
Each key uses a separate LazyValue; hence multiple keys can be in the
This expands on {LazyValue} by providing a lazy key-value dictionary.
@private
#
def await key, *extra_args, transient_errors: nil, max_tries: 1, max_time: nil
-
(Exception)
- if a fatal error happened, or retries have been
Returns:
-
(Object)
- the value
Parameters:
-
max_time
(Numeric, nil
) -- The maximum time in seconds this will -
max_tries
(Integer, nil
) -- The maximum number of times this will -
transient_errors
(Array
) -- An array of exception classes -
extra_args
(Array
) -- extra arguments to pass to the block -
key
(Object
) -- the key
def await key, *extra_args, transient_errors: nil, max_tries: 1, max_time: nil lookup_key(key).await key, *extra_args, transient_errors: transient_errors, max_tries: max_tries, max_time: max_time end
def expire! key
-
(true, false)
- whether the cache was expired
Parameters:
-
key
(Object
) -- the key
def expire! key lookup_key(key).expire! end
def expire_all!
-
(Array
- A list of keys that were expired. A key is
def expire_all! all_expired = [] @mutex.synchronize do @key_values.each do |key, value| all_expired << key if value.expire! end end all_expired end
def get key, *extra_args
-
(Exception)
- if an error happened while computing the value
Returns:
-
(Object)
- the value
Parameters:
-
extra_args
(Array
) -- extra arguments to pass to the block -
key
(Object
) -- the key
def get key, *extra_args lookup_key(key).get key, *extra_args end
def initialize retries: nil, &block
-
block
(Proc
) -- A block that can be called to attempt to compute -
retries
(Retries, Proc
) -- A retry manager. The default is a retry
def initialize retries: nil, &block @retries = retries @compute_handler = block @key_values = {} @mutex = Thread::Mutex.new end
def internal_state key
-
(Array)
- the low-level state
Parameters:
-
key
(Object
) -- the key
def internal_state key lookup_key(key).internal_state end
def lookup_key key
- Private: -
def lookup_key key # Optimization: check for key existence and return quickly without # grabbing the mutex. This works because keys are never deleted. return @key_values[key] if @key_values.key? key @mutex.synchronize do if @key_values.key? key @key_values[key] else retries = if @retries.respond_to? :reset_dup @retries.reset_dup elsif @retries.respond_to? :call @retries.call end @key_values[key] = LazyValue.new retries: retries, &@compute_handler end end end
def set! key, value, lifetime: nil
-
(Object)
- the value
Parameters:
-
lifetime
(Numeric
) -- the lifetime until expiration in seconds, -
value
(Object
) -- the value to set -
key
(Object
) -- the key
def set! key, value, lifetime: nil lookup_key(key).set! value, lifetime: lifetime end