mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Optimized persisting of retained messages to avoid useless writes to the filesystem
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Server;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
public class JsonServerStorage : IMqttServerStorage
|
||||
{
|
||||
private readonly string _filename = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Retained.json");
|
||||
|
||||
public async Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
var json = JsonConvert.SerializeObject(messages);
|
||||
File.WriteAllText(_filename, json);
|
||||
}
|
||||
|
||||
public async Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
if (!File.Exists(_filename))
|
||||
{
|
||||
return new List<MqttApplicationMessage>();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_filename);
|
||||
return JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<MqttApplicationMessage>();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (File.Exists(_filename))
|
||||
{
|
||||
File.Delete(_filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user