Skip to main content

Agent Data Persistence

Recommended Code Flow For Agent Data Persistence

The imp API provides a standard mechanism to allow agents to preserve data across device and service restarts, and to load that data back in at a later time.

To persist data, use the imp API method server.save(). Its accompanying method server.load() is used to retrieve the data.

The data to be preserved must be placed within a Squirrel table which is passed to server.save() as its only parameter. The table can be no larger than 64KB in size. While all Squirrel data types and entities may be stored in a table, only those which can be serialized may be preserved using server.save(). For more information on what Squirrel data can be serialized, see the Developer Guide ‘Data Serialization in Squirrel’.

Whenever the agent uses server.save(), any existing table is overwritten. The method returns an integer; 0 indicates a successful write. The preserved data can be cleared by passing an empty table to server.save().

Sample Code

The following code shows a typical flow. This involves loading the data at startup, assigning default values if this is the first time the agent has run, and saving modified values as they change.

By following this pattern of loading/initializing data at the top of the agent code, and saving it whenever it changes, you can easily ensure that your settings table and saved data remain in sync.

API Methods Used

Further Reading