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

Documentation for JSR223 cache preset #1937

Merged
merged 8 commits into from
Dec 4, 2022
Merged
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
72 changes: 72 additions & 0 deletions configuration/jsr223.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,78 @@ scriptExtension.importPreset("RuleFactories")
| `ConditionHandlerFactory` | `org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedConditionHandlerFactory` |
| `TriggerHandlerFactory` | `org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory` |

#### `cache` Preset

The `cache` preset does not provide a default import and needs to be imported explicitly.

:::: tabs

::: tab Groovy

```groovy
scriptExtension.importPreset("cache")
sharedCache.put("x", "y")
```

:::

::: tab Nashorn JS

```js
scriptExtension.importPreset("cache")

var valueX = sharedCache.get("x")
```

:::

::: tab JSScripting

```js
var { sharedCache, privateCache } = require('@runtime/cache')

sharedCache.remove("x")
```

:::

::: tab Jython

```python
scriptExtension.importPreset("cache")
```

:::

::: tab JRuby

```ruby
# the preset is imported automatically by the helper library.
value_x = shared_cache[:x]
shared_cache.delete(:x)
shared_cache[:x] = "y"
```

:::

::::

It provides two different caches:

- `sharedCache`: This cache is shared over all languages and all scripts (file-based and UI). Usage of entries is tracked and entries will be removed if the last script that ever accessed an entry is removed.
- `privateCache`: This cache is private to a script engine, usually that means it is private to a script or rule, depending on the implementation of the scripting language.
It is cleared when the script engine is unloaded (i.e. usually when the script is unloaded).

In both cases values that are either a `ScheduledFuture<?>` or a `Timer` are cancelled by calling `.cancel()` on the object if the object is removed automatically.

Both caches implement the `ValueCache` interface and therefore can be accessed by

- `Object put(String key, Object value)`: Put a key/value pair to the cache. Returns old value if already set, otherwise `null`.
- `Object remove(String key)`: Remove the key/value pair from the cache. Returns old value if already set, otherwise `null`.
- `Object get(String key)`: Get the value for the given key from the cache. Non-existent keys return `null`.
- `Object get(String key, Supplier<Object> supplier`: Get the value for the given key. If no value is present, add the value that is return from the `supplier`.

### `TriggerType` Objects (all JSR223 languages)

The following trigger types are defined by openHAB (custom triggers can also be defined) and take the specified configuration parameters.
Expand Down