FileSync + Cache
//Declare a cancellation token source to control the FSW.
//If using FSW within a ManagedThread, use the passed cancellation token
CancellationTokenSource CTS = new CancellationTokenSource();
//Local persist data
LocalPersistData localData = new LocalPersistData();
//Declare a new FileSyncWrite, it's generic T type can be any class that supports new()
FileSyncWrite<LocalPersistData> _FSW = new FileSyncWrite<LocalPersistData>($"sync{Path.DirectorySeparatorChar}local.json",
CTS.Token, //The token mentioned above
rollingDelay: 1000, //Rolling delay is bumped every time an Update() is pushed in.
maximumDelay: 10000, // Maximum delay is if rolling delay is never expired
// When either event is satisfied, the data is written to disk
(s,data) => { } //This is an updated event callback. Any time the data is written, this is called
);
//If initialized, then a value was loaded back in from the disk.
//If this is FALSE, no value was loaded in
if (_FSW.InitializedValue)
{
localData = _FSW.Get();
Console.WriteLine($"FSW Initialized: {localData.Count}, {localData.Offset}");
}
//Register an additional callback
_FSW.UpdatedEvent += (object s, LocalPersistData e) =>
{
if (e != null)
Console.WriteLine($"FSW Updated: {e.Count}, {e.Offset}");
};
//Push debounced updates to it:
localData.Count++;
_FSW.Update(localData);
//Tell the FSW to end all pending update queues and sync data back now
CTS.Cancel();
Task.Delay(8000).Wait();
return;Last updated

