Skip to content

Commit

Permalink
Minor corrections in documentation on timers and shared_cache (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Aug 25, 2023
1 parent 3eb6b47 commit 87bdda3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ It can be rescheduled to a different duration
```ruby
after(3.minutes) do |timer|
My_Light.on
my_timer.reschedule(1.minute)
timer.reschedule(1.minute)
end
```

Expand Down Expand Up @@ -922,22 +922,22 @@ Just be wary of Ruby-only data types (such as Symbols) that won't be accessible
Get a previously set object with a default value:

```ruby
counter_object = shared_cache.compute_if_absent(:counter) { { "times" => 0 } }
logger.info("Count: #{counter_object["times"] += 1}")"
shared_cache.compute_if_absent(:counter) { 0 } # Initialize with 0 if it didn't exist
logger.info("Count: #{shared_cache[:counter] += 1}")
```

Get a previously set object, or assign it (this version is subject to race conditions with other scripts):

```ruby
counter_object = shared_cache[:counter] ||= { "times" => 0 }
logger.info("Count: #{counter_object["times"] += 1}")"
shared_cache[:counter] ||= 0
logger.info("Count: #{shared_cache[:counter] += 1}")
```

Get a previously set object with a default value, without assigning it (this version has an even longer amount of time between fetchig the value and assigning it):
Get a previously set object with a default value, without assigning it (this version has an even longer amount of time between fetching the value and assigning it):

```ruby
count = shared_count.fetch(:counter) { 0 }
shared_count[:counter] = count + 1
count = shared_cache.fetch(:counter) { 0 }
shared_cache[:counter] = count + 1
```

### Time
Expand Down
2 changes: 1 addition & 1 deletion lib/openhab/core/value_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Core
# For a private cache, simply use an instance variable. See
# {file:docs/ruby-basics.md#variables Instance Variables}.
#
# @note Because every script or UI rule gets it own JRuby engine instance,
# @note 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
Expand Down

0 comments on commit 87bdda3

Please sign in to comment.