Module: OpenHAB::Core::ValueCache
- Defined in:
- lib/openhab/core/value_cache.rb
Overview
Only the sharedCache is exposed in Ruby. For a private cache, simply use an instance variable. See Instance Variables.
Because every script or UI rule gets its own JRuby engine instance, you cannot rely on being able to access Ruby objects between them. Only objects that implement a Java interface that's part of Java or openHAB Core (such as Hash implements java.util.Map, or other basic datatypes) can be reliably stored and accessed from the shared cache. Likewise, you can use the cache to access data from other scripting languages, but they'll be all but useless in Ruby. It's best to stick to simple data types. If you're having troubles, serializing to_json before storing may help.
ValueCache is the interface used to access a shared cache available between scripts and/or rule executions.
While ValueCache looks somewhat like a Hash, it does not support iteration of the contained elements. So it's limited to strictly storing, fetching, or removing known elements.
Shared caches are not persisted between openHAB restarts. And in fact, if all scripts are unloaded that reference a particular key, that key is removed.
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #store)
- #[]=(key, value) ⇒ Object
- #assoc(key) ⇒ Object
-
#compute(key) {|key, current_value| ... } ⇒ Object
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
-
#compute_if_absent(key) ⇒ Object
Compute and store new value for key if the key is absent.
- #delete(key) ⇒ Object
- #dig(key, *identifiers) ⇒ Object
- #fetch(key, *default_value) ⇒ Object
- #fetch_values(*keys, &block) ⇒ Object
- #key?(key) ⇒ Boolean (also: #has_key?, #include?, #member?)
- #merge!(*other_hashes) ⇒ Object (also: #update)
- #slice(*keys) ⇒ Object
- #to_proc ⇒ Object
- #values_at(*keys) ⇒ Object
Instance Method Details
#[](key) ⇒ Object Also known as: store
49 50 51 |
# File 'lib/openhab/core/value_cache.rb', line 49 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
78 79 80 |
# File 'lib/openhab/core/value_cache.rb', line 78 def []=(key, value) put(key, value) end |
#assoc(key) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/openhab/core/value_cache.rb', line 122 def assoc(key) [key, fetch(key) do # return nil directly, without storing a value to the cache return nil end] end |
#compute(key) {|key, current_value| ... } ⇒ Object
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). See java.util.Map#compute for details.
|
# File 'lib/openhab/core/value_cache.rb', line 53
|
#compute_if_absent(key) ⇒ Object
Compute and store new value for key if the key is absent. This method is atomic.
73 74 75 |
# File 'lib/openhab/core/value_cache.rb', line 73 def compute_if_absent(key, &) get(key, &) end |
#delete(key) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/openhab/core/value_cache.rb', line 84 def delete(key) key = key.to_s # needed for remove below if block_given? fetch(key) do return yield(key) end end remove(key) end |
#dig(key, *identifiers) ⇒ Object
131 132 133 134 |
# File 'lib/openhab/core/value_cache.rb', line 131 def dig(key, *identifiers) r = fetch(key) { return nil } r&.dig(*identifiers) end |
#fetch(key, *default_value) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/openhab/core/value_cache.rb', line 99 def fetch(key, *default_value) if default_value.length > 1 raise ArgumentError, "wrong number of arguments (given #{default_value.length + 1}, expected 0..1)" end if default_value.empty? key = key.to_s if block_given? get(key) do return yield(key) end else get(key) do raise KeyError.new("key not found: #{key.inspect}", key:) end end else get(key) { return default_value.first } end end |
#fetch_values(*keys, &block) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/openhab/core/value_cache.rb', line 137 def fetch_values(*keys, &block) result = [] keys.each do |key| if block result << fetch(key, &block) else has_value = true value = fetch(key) { has_value = false } result << value if has_value end end result end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?
152 153 154 |
# File 'lib/openhab/core/value_cache.rb', line 152 def key?(key) !!fetch(key) { return false } end |
#merge!(*other_hashes) ⇒ Object Also known as: update
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/openhab/core/value_cache.rb', line 160 def merge!(*other_hashes) other_hashes.each do |hash| hash.each do |(k, v)| k = k.to_s if block_given? dup = true old_value = fetch(k) do dup = false end self[k] = dup ? yield(k, old_value, v) : v else self[k] = v end end end self end |
#slice(*keys) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/openhab/core/value_cache.rb', line 180 def slice(*keys) result = {} keys.each do |k| k = k.to_s result[k] = self[k] if key?(k) end result end |
#to_proc ⇒ Object
190 191 192 |
# File 'lib/openhab/core/value_cache.rb', line 190 def to_proc ->(k) { self[k] } end |
#values_at(*keys) ⇒ Object
195 196 197 198 199 |
# File 'lib/openhab/core/value_cache.rb', line 195 def values_at(*keys) keys.map do |k| self[k] end end |