Module: OpenHAB::Core::ValueCache

Defined in:
lib/openhab/core/value_cache.rb

Overview

Note:

Only the sharedCache is exposed in Ruby. For a private cache, simply use an instance variable. See Instance Variables.

Note:

Because every script or UI rule gets it 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.

Examples:

shared_cache.compute_if_absent(:execution_count) { 0 }
shared_cache[:execution_count] += 1

See Also:

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object Also known as: store

See Also:



49
50
51
# File 'lib/openhab/core/value_cache.rb', line 49

def [](key)
  get(key.to_s)
end

#[]=(key, value) ⇒ Object

See Also:



65
66
67
# File 'lib/openhab/core/value_cache.rb', line 65

def []=(key, value)
  put(key.to_s, value)
end

#assoc(key) ⇒ Object

See Also:



109
110
111
112
113
114
# 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.

Parameters:

  • key (String)

Yield Returns:

  • (Object)

    new value

Returns:

  • (Object)

    new value or current value



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

See Also:



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

See Also:



117
118
119
120
# File 'lib/openhab/core/value_cache.rb', line 117

def dig(key, *identifiers)
  r = fetch(key) { return nil }
  r&.dig(*identifiers)
end

#fetch(key, *default_value) ⇒ Object

Examples:

shared_cache.fetch(:key_from_another_script) # raises NoKeyError

See Also:



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

See Also:



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/openhab/core/value_cache.rb', line 123

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?

Returns:

  • (Boolean)

See Also:



138
139
140
# File 'lib/openhab/core/value_cache.rb', line 138

def key?(key)
  !!fetch(key) { return false }
end

#merge!(*other_hashes) ⇒ Object Also known as: update

See Also:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/openhab/core/value_cache.rb', line 146

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

See Also:



166
167
168
169
170
171
172
173
# File 'lib/openhab/core/value_cache.rb', line 166

def slice(*keys)
  result = {}
  keys.each do |k|
    k = k.to_s
    result[k] = self[k] if key?(k)
  end
  result
end

#to_procObject

See Also:



176
177
178
# File 'lib/openhab/core/value_cache.rb', line 176

def to_proc
  @to_proc ||= ->(k) { self[k] }
end

#values_at(*keys) ⇒ Object

See Also:



181
182
183
184
185
# File 'lib/openhab/core/value_cache.rb', line 181

def values_at(*keys)
  keys.map do |k|
    self[k]
  end
end