diff --git a/configuration/jsr223.md b/configuration/jsr223.md index 26d7b5f8a8..e8f6086fc7 100644 --- a/configuration/jsr223.md +++ b/configuration/jsr223.md @@ -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 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.