Class: LazyData::Dict
- Inherits:
-
Object
- Object
- LazyData::Dict
- Defined in:
- lib/lazy_data/dict.rb
Overview
Instance Method Summary collapse
-
#await(key, *extra_args, transient_errors: nil, max_tries: 1, max_time: nil) ⇒ Object
This method calls #get repeatedly until a final result is available or retries have exhausted.
-
#expire!(key) ⇒ true, false
Force the cache for the given key to expire immediately, if computation is complete.
-
#expire_all! ⇒ Array<Object>
Force the values for all keys to expire immediately.
-
#get(key, *extra_args) ⇒ Object
(also: #[])
Returns the value for the given key.
-
#initialize(retries: nil, lifetime: nil, &block) ⇒ Dict
constructor
Create a LazyData::Dict.
-
#internal_state(key) ⇒ Array
Returns the current low-level state for the given key.
-
#set!(key, value, lifetime: nil) ⇒ Object
Set the cache value for the given key explicitly and immediately.
Constructor Details
#initialize(retries: nil, lifetime: nil, &block) ⇒ Dict
Create a LazyData::Dict.
You must pass a block that will be called to compute the value the first time it is accessed. The block takes the key as an argument and should evaluate to the value for that key, or raise an exception on error. To specify a value that expires, use LazyData.expiring_value. To raise an exception that expires, use LazyData.raise_expiring_error.
You can optionally pass a retry manager, which controls how subsequent accesses might try calling the block again if a compute attempt fails with an exception. A retry manager should either be an instance of Retries or an object that duck types it.
51 52 53 54 55 56 57 |
# File 'lib/lazy_data/dict.rb', line 51 def initialize(retries: nil, lifetime: nil, &block) @retries = retries @default_lifetime = lifetime @compute_handler = block @key_values = {} @mutex = ::Thread::Mutex.new end |
Instance Method Details
#await(key, *extra_args, transient_errors: nil, max_tries: 1, max_time: nil) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/lazy_data/dict.rb', line 104 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 |
#expire!(key) ⇒ true, false
Force the cache for the given key to expire immediately, if computation is complete.
Any cached value will be cleared, the retry count is reset, and the next access will call the compute block as if it were the first access. Returns true if this took place. Has no effect and returns false if the computation is not yet complete (i.e. if a thread is currently computing, or if the last attempt failed and retries have not yet been exhausted.)
135 136 137 |
# File 'lib/lazy_data/dict.rb', line 135 def expire!(key) lookup_key(key).expire! end |
#expire_all! ⇒ Array<Object>
Force the values for all keys to expire immediately.
147 148 149 150 151 152 153 154 155 |
# File 'lib/lazy_data/dict.rb', line 147 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 |
#get(key, *extra_args) ⇒ Object Also known as: []
Returns the value for the given key. This will either return the value or raise an error indicating failure to compute the value. If the value was previously cached, it will return that cached value, otherwise it will either run the computation to try to determine the value, or wait for another thread that is already running the computation.
Any arguments beyond the initial key argument will be passed to the block if it is called, but are ignored if a cached value is returned.
74 75 76 |
# File 'lib/lazy_data/dict.rb', line 74 def get(key, *extra_args) lookup_key(key).get(key, *extra_args) end |
#internal_state(key) ⇒ Array
Returns the current low-level state for the given key. Does not block for computation. See Value#internal_state for details.
118 119 120 |
# File 'lib/lazy_data/dict.rb', line 118 def internal_state(key) lookup_key(key).internal_state end |
#set!(key, value, lifetime: nil) ⇒ Object
Set the cache value for the given key explicitly and immediately. If a computation is in progress, it is "detached" and its result will no longer be considered.
168 169 170 |
# File 'lib/lazy_data/dict.rb', line 168 def set!(key, value, lifetime: nil) lookup_key(key).set!(value, lifetime: lifetime) end |