IMoBroPersistenceManager
The IMoBroPersistenceManager provides a simple way for plugins to persist arbitrary objects to disk.
Data is stored in the plugin's data directory and remains available between plugin starts.
Objects are persisted in JSON format and therefore must be serializable using System.Text.Json.JsonSerializer.
The data directory is part of the plugin's installation. All persisted data is deleted when the plugin is updated to a new version, reinstalled or uninstalled.
Storage Location
| Environment | Directory |
|---|---|
| Installed in MoBro | The data folder inside the plugin's installation directory. |
| Running locally | ./data, relative to the working directory. Can be changed using MoBroPluginBuilder.WithStorageDirectory. |
Example
public record LastRun(DateTime Timestamp, int ProcessedItems);
public class Plugin : IMoBroPlugin
{
private readonly IMoBroPersistenceManager _persistence;
public Plugin(IMoBroPersistenceManager persistence)
{
_persistence = persistence;
}
public void Init()
{
// null if nothing has been stored yet
var lastRun = _persistence.Get<LastRun>("last_run");
_persistence.Put("last_run", new LastRun(DateTime.UtcNow, lastRun?.ProcessedItems + 1 ?? 1));
}
}
Functions
Put<T>(string, T)
Stores the data under the specified key. Existing data with the same key is replaced.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique key to identify the data. |
| data | T | The data to be stored. |
Get<T>(string): T?
Retrieves the data stored under the specified key.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The key to identify the stored data. |
Returns
- The stored data if it exists.
default(T)if the key is not found, or if the data was stored as a different type thanT.
Always retrieve data using the same type it was stored with.
Remove(string): bool
Removes the data stored under the specified key.
Returns
true if data was removed; false if the key does not exist.
Exists(string): bool
Checks whether data is stored under the specified key.
Returns
true if data with the specified key exists; otherwise false.
Clear()
Deletes all persisted data.