Help with shared cache in dsl

No.

The problem is if the Thing goes offline, even if only briefly, and then comes back online that variable will no longer be valid. You need to pull it from the Thing again.

It’s best to pull the actions in the rule(s) that use them just before calling it.

The same logic applies to global variables. You should not store actions in global variables either for the same reasons.

If the timer is only used in one rule, use the privateCache.

You don’t initialize entries to null in the cache. Non-existent keys already return null. But even if it did, the cache doesn’t care if it’s a timer. It just holds any arbitrary object. You need to know it’s a timer when you retrieve the value.

So you just need to put your new timer to the cache when you create it. When you get the timer from the cache, and it’snull you know the timer doesn’t exist. To remove the timer from the cache put null for that key.

val timer = privateCache.get('myTimer')
if(timer === null) {
    // timer doesn't exist
    privateCache.put('myTimer', createTimer(time, [ | 
        // do something
        privateCache.put('myTimer', null) // I think there is also a remove method
    ]))
}
else {
    timer.reschedule(time)
}

This approach is further described in Design Pattern: Separation of Behaviors. It’s a great approach to centralize stuff like notifications so it’s easier to change things (e.g. change from email to Telegram or the like). There’s only one place to make the change.

Performance differences would be unmeasurable.