You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-16
Original file line number
Diff line number
Diff line change
@@ -859,48 +859,47 @@ See [openhab-js : actions.NotificationBuilder](https://openhab.github.io/openhab
859
859
860
860
### Cache
861
861
862
-
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.
862
+
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.
863
863
864
864
The private cache can only be accessed by the same script and is cleared when the script is unloaded.
865
-
You can use it to e.g. store timers or counters between subsequent runs of that script.
865
+
You can use it to store both primitives and objects, e.g. store timers or counters between subsequent runs of that script.
866
866
When a script is unloaded and its cache is cleared, all timers (see [`createTimer`](#createtimer)) stored in its private cache are automatically cancelled.
867
867
868
868
The shared cache is shared across all rules and scripts, it can therefore be accessed from any automation language.
869
869
The access to every key is tracked and the key is removed when all scripts that ever accessed that key are unloaded.
870
870
If that key stored a timer, the timer will be cancelled.
871
+
You can use it to store **only primitives**, as storing objects is not thread-safe and can cause script execution failures.
871
872
872
873
See [openhab-js : cache](https://openhab.github.io/openhab-js/cache.html) for full API documentation.
* The {@link JSCache} can be used by to share information between subsequent runs of the same script or between scripts (depending on implementation).
11
11
*/
12
12
classJSCache{
13
+
#valueCache;
14
+
13
15
/**
14
16
* @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
15
17
* @hideconstructor
16
18
*/
17
19
constructor(valueCacheImpl){
18
-
this._valueCache=valueCacheImpl;
20
+
this.#valueCache =valueCacheImpl;
21
+
}
22
+
23
+
#isSharedCache (){
24
+
returnthis.#valueCache ===sharedCache;
19
25
}
20
26
21
27
/**
@@ -27,9 +33,9 @@ class JSCache {
27
33
*/
28
34
get(key,defaultSupplier){
29
35
if(typeofdefaultSupplier==='function'){
30
-
returnthis._valueCache.get(key,defaultSupplier);
36
+
returnthis.#valueCache.get(key,defaultSupplier);
31
37
}else{
32
-
returnthis._valueCache.get(key);
38
+
returnthis.#valueCache.get(key);
33
39
}
34
40
}
35
41
@@ -41,7 +47,10 @@ class JSCache {
41
47
* @returns {*|null} the previous value associated with the key, or null if there was no mapping for key
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!`);
52
+
}
53
+
returnthis.#valueCache.put(key,value);
45
54
}
46
55
47
56
/**
@@ -51,7 +60,7 @@ class JSCache {
51
60
* @returns {*|null} the previous value associated with the key or null if there was no mapping for key
52
61
*/
53
62
remove(key){
54
-
returnthis._valueCache.remove(key);
63
+
returnthis.#valueCache.remove(key);
55
64
}
56
65
57
66
/**
@@ -61,7 +70,7 @@ class JSCache {
61
70
* @returns {boolean} whether the key has a mapping
62
71
*/
63
72
exists(key){
64
-
returnthis._valueCache.get(key)!==null;
73
+
returnthis.#valueCache.get(key)!==null;
65
74
}
66
75
}
67
76
@@ -71,6 +80,8 @@ module.exports = {
71
80
* The access to every key is tracked and the key is removed when all scripts that ever accessed that key are unloaded.
72
81
* If the key that has been auto-removed stored a timer, that timer is cancelled.
73
82
*
83
+
* Do not use the shared cache to store objects, as it is not thread-safe and can cause script execution failures.
0 commit comments