Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor corrections in documentation on timers and shared_cache #118

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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