Skip to content

Commit

Permalink
[cache] Advise to not store objects in shared cache (#391)
Browse files Browse the repository at this point in the history
Update docs and log a warning if done so.

Refs openhab/openhab-core#4413.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Oct 24, 2024
1 parent e4ec842 commit a0628ae
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 29 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,48 +859,47 @@ See [openhab-js : actions.NotificationBuilder](https://openhab.github.io/openhab
### Cache
The cache namespace provides both a private and a shared cache that can be used to set and retrieve objects that will be persisted between subsequent runs of the same or between scripts.
The cache namespace provides both a private and a shared cache that can be used to set and retrieve data that will be persisted between subsequent runs of the same or between scripts.
The private cache can only be accessed by the same script and is cleared when the script is unloaded.
You can use it to e.g. store timers or counters between subsequent runs of that script.
You can use it to store both primitives and objects, e.g. store timers or counters between subsequent runs of that script.
When a script is unloaded and its cache is cleared, all timers (see [`createTimer`](#createtimer)) stored in its private cache are automatically cancelled.
The shared cache is shared across all rules and scripts, it can therefore be accessed from any automation language.
The access to every key is tracked and the key is removed when all scripts that ever accessed that key are unloaded.
If that key stored a timer, the timer will be cancelled.
You can use it to store **only primitives**, as storing objects is not thread-safe and can cause script execution failures.
See [openhab-js : cache](https://openhab.github.io/openhab-js/cache.html) for full API documentation.
- cache : <code>object</code>
- .private
- .get(key, defaultSupplier) ⇒ <code>Object | null</code>
- .put(key, value) ⇒ <code>Previous Object | null</code>
- .remove(key) ⇒ <code>Previous Object | null</code>
- .get(key, defaultSupplier) ⇒ <code>* | null</code>
- .put(key, value) ⇒ <code>Previous * | null</code>
- .remove(key) ⇒ <code>Previous * | null</code>
- .exists(key) ⇒ <code>boolean</code>
- .shared
- .get(key, defaultSupplier) ⇒ <code>Object | null</code>
- .put(key, value) ⇒ <code>Previous Object | null</code>
- .remove(key) ⇒ <code>Previous Object | null</code>
- .get(key, defaultSupplier) ⇒ <code>* | null</code>
- .put(key, value) ⇒ <code>Previous * | null</code>
- .remove(key) ⇒ <code>Previous * | null</code>
- .exists(key) ⇒ <code>boolean</code>
The `defaultSupplier` provided function will return a default value if a specified key is not already associated with a value.
**Example** *(Get a previously set value with a default value (times = 0))*
```js
var counter = cache.private.get('counter', () => ({ 'times': 0 }));
console.log('Count', counter.times++);
var counter = cache.shared.get('counter', () => 0);
console.log('Counter: ' + counter);
```
**Example** *(Get a previously set object)*
**Example** *(Get a previously set value, modify and store it)*
```js
var counter = cache.private.get('counter');
if (counter === null) {
counter = { times: 0 };
cache.private.put('counter', counter);
}
console.log('Count', counter.times++);
counter++;
console.log('Counter: ' + counter);
cache.private.put('counter', counter);
```
### Time
Expand Down
23 changes: 17 additions & 6 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ const { privateCache, sharedCache } = require('@runtime/cache');
* The {@link JSCache} can be used by to share information between subsequent runs of the same script or between scripts (depending on implementation).
*/
class JSCache {
#valueCache;

/**
* @param {*} valueCacheImpl an implementation of the Java {@link https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/shared/ValueCache.java ValueCache} interface
* @hideconstructor
*/
constructor (valueCacheImpl) {
this._valueCache = valueCacheImpl;
this.#valueCache = valueCacheImpl;
}

#isSharedCache () {
return this.#valueCache === sharedCache;
}

/**
Expand All @@ -27,9 +33,9 @@ class JSCache {
*/
get (key, defaultSupplier) {
if (typeof defaultSupplier === 'function') {
return this._valueCache.get(key, defaultSupplier);
return this.#valueCache.get(key, defaultSupplier);
} else {
return this._valueCache.get(key);
return this.#valueCache.get(key);
}
}

Expand All @@ -41,7 +47,10 @@ class JSCache {
* @returns {*|null} the previous value associated with the key, or null if there was no mapping for key
*/
put (key, value) {
return this._valueCache.put(key, value);
if (typeof value === 'object' && this.#isSharedCache()) {
console.warn(`Do not use the shared cache to store the object with key '${key}', as it is not thread-safe and can cause script execution failures. Only store primitives in the shared cache!`);
}
return this.#valueCache.put(key, value);
}

/**
Expand All @@ -51,7 +60,7 @@ class JSCache {
* @returns {*|null} the previous value associated with the key or null if there was no mapping for key
*/
remove (key) {
return this._valueCache.remove(key);
return this.#valueCache.remove(key);
}

/**
Expand All @@ -61,7 +70,7 @@ class JSCache {
* @returns {boolean} whether the key has a mapping
*/
exists (key) {
return this._valueCache.get(key) !== null;
return this.#valueCache.get(key) !== null;
}
}

Expand All @@ -71,6 +80,8 @@ module.exports = {
* The access to every key is tracked and the key is removed when all scripts that ever accessed that key are unloaded.
* If the key that has been auto-removed stored a timer, that timer is cancelled.
*
* Do not use the shared cache to store objects, as it is not thread-safe and can cause script execution failures.
*
* @memberof cache
* @type JSCache
*/
Expand Down
2 changes: 1 addition & 1 deletion types/cache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class JSCache {
* @hideconstructor
*/
constructor(valueCacheImpl: any);
_valueCache: any;
/**
* Returns the value to which the specified key is mapped.
*
Expand Down Expand Up @@ -38,6 +37,7 @@ export class JSCache {
* @returns {boolean} whether the key has a mapping
*/
exists(key: string): boolean;
#private;
}
export declare const shared: JSCache;
declare const _private: JSCache;
Expand Down
2 changes: 1 addition & 1 deletion types/cache.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/items/metadata/itemchannellink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Item = {
sendCommandIfDifferent(value: any): boolean;
sendIncreaseCommand(value: any): boolean;
sendDecreaseCommand(value: any): boolean;
"__#5@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
"__#6@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
Expand Down
2 changes: 1 addition & 1 deletion types/items/metadata/metadata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type Item = {
sendCommandIfDifferent(value: any): boolean;
sendIncreaseCommand(value: any): boolean;
sendDecreaseCommand(value: any): boolean;
"__#5@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
"__#6@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
Expand Down
2 changes: 1 addition & 1 deletion types/quantity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type Item = {
sendCommandIfDifferent(value: any): boolean;
sendIncreaseCommand(value: any): boolean;
sendDecreaseCommand(value: any): boolean;
"__#5@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
"__#6@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
Expand Down
2 changes: 1 addition & 1 deletion types/rules/operation-builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Item = {
sendCommandIfDifferent(value: any): boolean;
sendIncreaseCommand(value: any): boolean;
sendDecreaseCommand(value: any): boolean;
"__#5@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
"__#6@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
Expand Down
2 changes: 1 addition & 1 deletion types/triggers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Item = {
sendCommandIfDifferent(value: any): boolean;
sendIncreaseCommand(value: any): boolean;
sendDecreaseCommand(value: any): boolean;
"__#5@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
"__#6@#getToggleState"(): "PAUSE" | "PLAY" | "OPEN" | "CLOSED" | "ON" | "OFF";
sendToggleCommand(): void;
postToggleUpdate(): void;
postUpdate(value: any): void;
Expand Down

0 comments on commit a0628ae

Please sign in to comment.