FSW for short is a class suited for synchronizing data to the local drive. In short, you supply the CancellationToken from the thread and it safely handles the file locking and writing of newly updated data on a regularly scheduled basis and before the application closes.
It's much lighter than the ConcurrentFileStore and FileRevisionStore, as such, there's no transactional lock and write verification fallbacks. The plus side is that there's fewer read/writes and if those features aren't of interest to you, this would be the next best choice!
If you run the below demo multiple times, you'll see the count being restored, incremented and shut down. There's a new file with the data located at bin\debug\sync\local.json.
//Declare a cancellation token source to control the FSW. //If using FSW within a ManagedThread, use the passed cancellation tokenCancellationTokenSource CTS =newCancellationTokenSource();//Local persist dataLocalPersistData localData =newLocalPersistData();//Declare a new FileSyncWrite, it's generic T type can be any class that supports new()FileSyncWrite<LocalPersistData> _FSW =newFileSyncWrite<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 inif (_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 nowCTS.Cancel();Task.Delay(8000).Wait();return;
The class used above is simply two properties with getter/setters: