Introduce pluggable snapshot storage for persistence

This commit is contained in:
Jacob Dubin
2026-05-17 07:02:49 -05:00
parent 785dc2b48b
commit 888f472f69
5 changed files with 67 additions and 8 deletions

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -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();
}

View File

@@ -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))
{