Introduce pluggable snapshot storage for persistence
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Jibo.Cloud.Infrastructure.Persistence;
|
||||
|
||||
public interface ISnapshotStore
|
||||
{
|
||||
TSnapshot? Load<TSnapshot>() where TSnapshot : class;
|
||||
void Save<TSnapshot>(TSnapshot snapshot) where TSnapshot : class;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
private readonly ConcurrentDictionary<string, CloudSession> _sessionsByToken = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ConcurrentDictionary<string, string> _symmetricKeys = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ConcurrentDictionary<string, KeyRequestRecord> _keyRequests = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly JsonSnapshotStore _snapshotStore;
|
||||
private readonly ISnapshotStore _snapshotStore;
|
||||
private readonly Lock _syncRoot = new();
|
||||
private readonly List<UpdateManifest> _updates;
|
||||
private readonly List<MediaRecord> _media = [];
|
||||
@@ -32,8 +32,13 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
private DateTimeOffset? _lastSavedUtc;
|
||||
|
||||
public InMemoryCloudStateStore(string? persistencePath = null)
|
||||
: this(new JsonFileSnapshotStore(persistencePath, PersistenceJsonOptions))
|
||||
{
|
||||
_snapshotStore = new JsonSnapshotStore(persistencePath, PersistenceJsonOptions);
|
||||
}
|
||||
|
||||
public InMemoryCloudStateStore(ISnapshotStore snapshotStore)
|
||||
{
|
||||
_snapshotStore = snapshotStore;
|
||||
_robot = new DeviceRegistration
|
||||
{
|
||||
HostMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
|
||||
@@ -13,15 +13,20 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
};
|
||||
|
||||
private readonly ConcurrentDictionary<string, TenantMemoryRecord> _tenantMemory = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly JsonSnapshotStore _snapshotStore;
|
||||
private readonly ISnapshotStore _snapshotStore;
|
||||
private readonly Lock _syncRoot = new();
|
||||
private long _revision;
|
||||
private DateTimeOffset? _lastLoadedUtc;
|
||||
private DateTimeOffset? _lastSavedUtc;
|
||||
|
||||
public InMemoryPersonalMemoryStore(string? persistencePath = null)
|
||||
: this(new JsonFileSnapshotStore(persistencePath, PersistenceJsonOptions))
|
||||
{
|
||||
_snapshotStore = new JsonSnapshotStore(persistencePath, PersistenceJsonOptions);
|
||||
}
|
||||
|
||||
public InMemoryPersonalMemoryStore(ISnapshotStore snapshotStore)
|
||||
{
|
||||
_snapshotStore = snapshotStore;
|
||||
LoadPersistedState();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,18 @@ using System.Text.Json;
|
||||
|
||||
namespace Jibo.Cloud.Infrastructure.Persistence;
|
||||
|
||||
internal sealed class JsonSnapshotStore
|
||||
internal sealed class JsonFileSnapshotStore : ISnapshotStore
|
||||
{
|
||||
private readonly string? _persistencePath;
|
||||
private readonly JsonSerializerOptions _options;
|
||||
|
||||
public JsonSnapshotStore(string? persistencePath, JsonSerializerOptions options)
|
||||
public JsonFileSnapshotStore(string? persistencePath, JsonSerializerOptions options)
|
||||
{
|
||||
_persistencePath = persistencePath;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public TSnapshot? Load<TSnapshot>()
|
||||
public TSnapshot? Load<TSnapshot>() where TSnapshot : class
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath) || !File.Exists(_persistencePath))
|
||||
{
|
||||
@@ -30,7 +30,7 @@ internal sealed class JsonSnapshotStore
|
||||
}
|
||||
}
|
||||
|
||||
public void Save<TSnapshot>(TSnapshot snapshot)
|
||||
public void Save<TSnapshot>(TSnapshot snapshot) where TSnapshot : class
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath))
|
||||
{
|
||||
Reference in New Issue
Block a user