Skip to content

Commit

Permalink
Fix shared_cache examples
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Aug 25, 2023
1 parent 569eeac commit e927ebd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions USAGE.md
Original file line number Diff line number Diff line change
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 e927ebd

Please sign in to comment.