Skip to content

Commit afca4cf

Browse files
committed
WIP: Update IAsyncStorageBackend.cs and MnemonicDBStorageBackend.cs to accept key scopes
1 parent e83baf4 commit afca4cf

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/NexusMods.Backend/Settings/MnemonicDBStorageBackend.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ public MnemonicDBStorageBackend(IServiceProvider serviceProvider)
5252
}
5353
}
5454

55-
private static string GetId<T>() where T : ISettings
55+
private static string GetId<T>(string? key) where T : ISettings
5656
{
57-
return typeof(T).FullName ?? typeof(T).Name;
57+
var typeName = typeof(T).FullName ?? typeof(T).Name;
58+
return string.IsNullOrWhiteSpace(key) ? typeName : $"{typeName}|{key}";
5859
}
5960

60-
public async ValueTask Save<T>(T value, CancellationToken cancellationToken) where T : class, ISettings, new()
61+
public async ValueTask Save<T>(T value, string? key, CancellationToken cancellationToken) where T : class, ISettings, new()
6162
{
6263
var db = _conn.Value.Db;
6364
using var tx = _conn.Value.BeginTransaction();
6465

65-
var name = GetId<T>();
66+
var name = GetId<T>(key);
6667

6768
EntityId id;
6869
var settings = Setting.FindByName(db, name).ToArray();
@@ -80,9 +81,9 @@ private static string GetId<T>() where T : ISettings
8081
await tx.Commit();
8182
}
8283

83-
public ValueTask<T?> Load<T>(CancellationToken cancellationToken) where T : class, ISettings, new()
84+
public ValueTask<T?> Load<T>(string? key, CancellationToken cancellationToken) where T : class, ISettings, new()
8485
{
85-
var settings = Setting.FindByName(_conn.Value.Db, GetId<T>()).ToArray();
86+
var settings = Setting.FindByName(_conn.Value.Db, GetId<T>(key)).ToArray();
8687
if (settings.Length == 0) return ValueTask.FromResult<T?>(null);
8788
return ValueTask.FromResult(Deserialize<T>(settings.First().Value));
8889
}

src/NexusMods.Sdk/Settings/Backend/IAsyncStorageBackend.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IAsyncStorageBackend : IBaseStorageBackend, IAsyncDisposable
1212
/// <summary>
1313
/// Saves the given settings object to the storage backend asynchronously.
1414
/// </summary>
15-
ValueTask Save<T>(T value, CancellationToken cancellationToken) where T : class, ISettings, new();
15+
ValueTask Save<T>(T value, string? key, CancellationToken cancellationToken) where T : class, ISettings, new();
1616

1717
/// <summary>
1818
/// Loads the given settings type from the storage backend asynchronously.
@@ -21,5 +21,5 @@ public interface IAsyncStorageBackend : IBaseStorageBackend, IAsyncDisposable
2121
/// Either the loaded value or <c>null</c> if the loading failed
2222
/// or the storage backend doesn't contain this value.
2323
/// </returns>
24-
ValueTask<T?> Load<T>(CancellationToken cancellationToken) where T : class, ISettings, new();
24+
ValueTask<T?> Load<T>(string? key, CancellationToken cancellationToken) where T : class, ISettings, new();
2525
}

0 commit comments

Comments
 (0)