Questions about persistent context

I want to use persistent context in one of my contributed nodes, but I have a few questions about how it works.

First, when using the localfilesystem module, the documentation says the default flushInterval is 30 seconds. (Good for the health of SD cards.) Does this mean that the entire cache is flushed every 30 seconds, regardless of whether any of its contents have changed? It would be nice (at least for testing with small amounts of data) to be able to set the interval much shorter without having to worry.

Second, am I right to assume that "flush" here just means refreshing the filesystem copy, so that the copy in memory continues to be used (unless cache is false)?

Third, is there a way to force a flush from memory to "disk", so that the persistent copy is guaranteed to be up to date?

Finally, I want to assume that NR flushes the cache when it shuts down. Is that correct?

Sorry to be a bother, but trial-and-error has taken me only so far.

2 Likes

To begin with, as a node author, you just use Context. You don't get to pick if its persistent or not as that is up to the end user.

No. It only flushes when there are changes to flush, but will ensures it doesn't flush any more frequently than the configured interval.

Right.

No. The context api only lets you get/set values - that is all. You don't know what instance of context plugin the user has configured.

Yes. Assuming NR is shutdown cleanly, then the filesystem context plugin flushes its contents as part of its closing down. If NR is abruptly halted then any unflushed data will be lost.

The node in question is node-red-simple-gate, and the aim is to persist the node's state in the event of a reboot/ node-RED restart, etc. So saving the state to memory would not be effective, it would need to be saved to disk, so that it could be re-loaded, to persist the gate's state.

That doesn't change the fact that a node just uses context and it is up to the end user to choose how that is configured and whether it will be persistent or not.

@knolleary Thanks for the information. I think that tells me what I need to know.

Understood. As @Paul-Reed says, the immediate question involves his issue with node-red-contrib-simple-gate. The goal is not to guarantee the user any particular kind of persistence but to give him the option of using it if available. The node itself will have to determine whether it can get what it needs from the store or should do something else.