Context Storage: filesystem

I have the need to store a variable in the local filesystem context:

flow.set("lastrike", Date.now() - 86400000 * 180); // default is 180 days - stored in volatile memory

I have modified settings.js for the following variable store's:

	contextStorage: {
		default: "memoryOnly",
		memoryOnly: { module: 'memory' },
		disk: {
			module: 'localfilesystem' ,
			config: { flushInterval: '30' },
		}
	}

I want to store the variable on the disk store and only call it when Node-RED is restarted. At that point I want to use the default store for processing the data. I only want to update the disk store every 5 minutes.

I am not sure which store to be writing to or calling.

image

What is the proper way to implement disk store? I am not sure how flushInterval will automatically write the default store to disk store.

Thanks

If you write to context using a function you can name the store to use

flow.set("name_of_var", "data","disk")
flow.get"name_of_var", "disk")

in a change node you select the stoe to the right of the rule.


If using JSONata it is similar to function node.

$flowContext("name_of_var", "disk")

What the docs say Working with context : Node-RED

If you want to write the default store to disk every 5 mins just set up a flow to do that.

Where I am confused if I write to 'disk' will the flushInterval only update the actual store based on the setting (i.e = 30 seconds). If i have to create a flow to push the default store to disk then how does flushInterval fit into this?

I believe 30 is default.
This will not copy default store to disk store. They are separate stores. To copy default to disk every 5 mins set up a flow to do this.

Your last post is where I was heading. However I dont want to fetch from the disk so I will need to used both stores to keep load off the file system.

thx

If you write it to the store called disk then that is where you must read it from. It will only read the disk once as it keeps a cached copy for you to use. It only writes to disk at the interval that you specify and only then if the value has been written to since the last flush.
Don't think of it as a store on disk, think of it as a store in memory that is backed up on disk when required and restored on startup.

1 Like

Further to my last post, use the default (memory) store for data that does not need to be restored on startup, and use the disk backed store for data that does need to be restored.

However, it is good practice to minimise the use of context. Wherever possible send data using messages and only store in context when there is a real advantage to doing that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.