47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
#if NET
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
|
|
|
namespace EnvelopeGenerator.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// Implements user preferences storage using the distributed cache (dbo.TBDD_CACHE).
|
|
/// No DB schema changes required — uses the existing SQL Server distributed cache table.
|
|
/// Cache key schema: "signflow.user-preferences:{username}"
|
|
/// Expiration: indefinite (no TTL) — preferences persist until explicitly overwritten.
|
|
/// </summary>
|
|
public sealed class UserPreferencesService(IDistributedCache cache) : IUserPreferencesService
|
|
{
|
|
private const string KeyPrefix = "signflow.user-preferences:";
|
|
|
|
private static string BuildKey(string id) => $"{KeyPrefix}{id}";
|
|
|
|
/// <inheritdoc />
|
|
public async Task<UserPreferencesDto> GetOrCreateAsync(string id, CancellationToken ct = default)
|
|
{
|
|
var json = await cache.GetStringAsync(BuildKey(id), ct);
|
|
if(json is not null && JsonSerializer.Deserialize<UserPreferencesDto>(json) is UserPreferencesDto pref)
|
|
{
|
|
return pref;
|
|
}
|
|
else
|
|
{
|
|
// init new
|
|
await SaveAsync(id, UserPreferencesDto.Default, ct);
|
|
return UserPreferencesDto.Default;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task SaveAsync(string id, UserPreferencesDto preferences, CancellationToken ct = default)
|
|
{
|
|
var json = JsonSerializer.Serialize(preferences);
|
|
|
|
// No expiration — preferences are stored indefinitely.
|
|
await cache.SetStringAsync(BuildKey(id), json, new DistributedCacheEntryOptions(), ct);
|
|
}
|
|
}
|
|
#endif
|