Files
hrynco-common/HrynCo.Common/Caching/InMemorySessionCredentialStore.cs
T
Anatolii Grynchuk 85b362e8cd chore: add hrynco common library solution
- add the standalone HrynCo.Common solution and projects
- include the shared common library source and tests
- add package metadata and a repo gitignore
2026-05-01 00:17:34 +03:00

56 lines
1.5 KiB
C#

namespace HrynCo.Common.Caching;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
public sealed class InMemorySessionCredentialStore : ISessionCredentialStore
{
private readonly IMemoryCache _cache;
public InMemorySessionCredentialStore(IMemoryCache cache)
{
_cache = cache;
}
public Task<string?> GetAsync(Guid userId, string provider, CancellationToken cancellationToken = default)
{
_ = cancellationToken;
_cache.TryGetValue(BuildKey(userId, provider), out string? apiKey);
return Task.FromResult(apiKey);
}
public Task RemoveAsync(Guid userId, string provider, CancellationToken cancellationToken = default)
{
_ = cancellationToken;
_cache.Remove(BuildKey(userId, provider));
return Task.CompletedTask;
}
public Task SaveAsync(
Guid userId,
string provider,
string apiKey,
TimeSpan ttl,
CancellationToken cancellationToken = default)
{
_ = cancellationToken;
_cache.Set(
BuildKey(userId, provider),
apiKey,
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = ttl,
SlidingExpiration = TimeSpan.FromMinutes(30)
});
return Task.CompletedTask;
}
private static string BuildKey(Guid userId, string provider)
{
return $"ai-session-key:{provider}:{userId:N}";
}
}