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_if_absent(key, &block) ⇒ 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.to_s) end |
#[]=(key, value) ⇒ Object
65 66 67 |
# File 'lib/openhab/core/value_cache.rb', line 65 def []=(key, value) put(key.to_s, value) end |
#assoc(key) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/openhab/core/value_cache.rb', line 109 def assoc(key) [key, fetch(key) do # return nil directly, without storing a value to the cache return nil end] end |
#compute_if_absent(key, &block) ⇒ Object
Compute and store new value for key if the key is absent. This method is atomic.
60 61 62 |
# File 'lib/openhab/core/value_cache.rb', line 60 def compute_if_absent(key, &block) get(key.to_s, &block) end |
#delete(key) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/openhab/core/value_cache.rb', line 71 def delete(key) key = key.to_s if block_given? fetch(key) do return yield(key) end end remove(key) end |
#dig(key, *identifiers) ⇒ Object
118 119 120 121 |
# File 'lib/openhab/core/value_cache.rb', line 118 def dig(key, *identifiers) r = fetch(key) { return nil } r&.dig(*identifiers) end |
#fetch(key, *default_value) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openhab/core/value_cache.rb', line 86 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 key = key.to_s if default_value.empty? if block_given? get(key) do return yield(key) end else get(key) do raise KeyError.new("key not found: #{key.inspect}", key: key) end end else get(key) { return default_value.first } end end |
#fetch_values(*keys, &block) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/openhab/core/value_cache.rb', line 124 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?
139 140 141 |
# File 'lib/openhab/core/value_cache.rb', line 139 def key?(key) !!fetch(key) { return false } end |
#merge!(*other_hashes) ⇒ Object Also known as: update
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/openhab/core/value_cache.rb', line 147 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
167 168 169 170 171 172 173 174 |
# File 'lib/openhab/core/value_cache.rb', line 167 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
177 178 179 |
# File 'lib/openhab/core/value_cache.rb', line 177 def to_proc @to_proc ||= ->(k) { self[k] } end |
#values_at(*keys) ⇒ Object
182 183 184 185 186 |
# File 'lib/openhab/core/value_cache.rb', line 182 def values_at(*keys) keys.map do |k| self[k] end end |