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
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
namespace HrynCo.Common.Caching;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface ISessionCredentialStore
|
||||
{
|
||||
Task<string?> GetAsync(Guid userId, string provider, CancellationToken cancellationToken = default);
|
||||
|
||||
Task RemoveAsync(Guid userId, string provider, CancellationToken cancellationToken = default);
|
||||
|
||||
Task SaveAsync(
|
||||
Guid userId,
|
||||
string provider,
|
||||
string apiKey,
|
||||
TimeSpan ttl,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace HrynCo.Common.Caching;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface ISessionPromptStore
|
||||
{
|
||||
Task<string?> GetAsync(Guid userId, string provider, CancellationToken cancellationToken = default);
|
||||
|
||||
Task RemoveAsync(Guid userId, string provider, CancellationToken cancellationToken = default);
|
||||
|
||||
Task SaveAsync(
|
||||
Guid userId,
|
||||
string provider,
|
||||
string prompt,
|
||||
TimeSpan ttl,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
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}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
namespace HrynCo.Common.Caching;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
public sealed class InMemorySessionPromptStore : ISessionPromptStore
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public InMemorySessionPromptStore(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public Task<string?> GetAsync(Guid userId, string provider, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_ = cancellationToken;
|
||||
_cache.TryGetValue(BuildKey(userId, provider), out string? prompt);
|
||||
return Task.FromResult(prompt);
|
||||
}
|
||||
|
||||
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 prompt,
|
||||
TimeSpan ttl,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
_ = cancellationToken;
|
||||
_cache.Set(
|
||||
BuildKey(userId, provider),
|
||||
prompt,
|
||||
new MemoryCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpirationRelativeToNow = ttl,
|
||||
SlidingExpiration = TimeSpan.FromMinutes(30)
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static string BuildKey(Guid userId, string provider)
|
||||
{
|
||||
return $"ai-session-prompt:{provider}:{userId:N}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user