Files
Khaemhat/src/Khaemhat.Core/Caching/ICacheProvider.cs
T
Alby96andClaude Opus 5.5 032c602b49
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s
feat: system and security - start-up, accounts, sign-in, licences, log, jobs, companies (M2)
The instance now starts on its own from KHAEMHAT_* variables: it migrates the system
and company databases, creates the super user and the first company, and only then
reports ready. Users sign in with password and TOTP through revocable database
sessions, and licences decide which modules and how many companies are available.

- Configuration: every wrong variable reported at once (exit code 2), _FILE variants
- Accounts, password policy and lockout, TOTP with recovery codes, session states
  with token rotation, security audit with a hash chain
- Sign-in pages (mockups 001-003), authorization attributes, security headers and a
  CSP without inline scripts, login rate limit
- Signed licences, activation codes, test mode, licence tool
- DataProtection keys in the system database, encrypted with the instance key
- Memory or Redis cache (in-house RESP client) that falls back to memory
- Database log by section with trace id; cron scheduler with locks and system jobs;
  record history (audit_log)
- Company creation, states, company context and X-Company, 503 in maintenance
- In-memory SQLite databases stay alive for the process (tests, demo)
- Tests for all of the above, ADR 0019, wiki, SECURITY, CHANGELOG, STATUS, DATA-MODEL

Refs #15 #16 #17 #18 #19 #20 #21 #22 #23 #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:29:24 +01:00

81 lines
4.0 KiB
C#

namespace Khaemhat.Core.Caching;
/// <summary>
/// The application cache (SPEC §9): in memory, or Redis when <c>KHAEMHAT_REDIS</c> is set. Values are stored as JSON
/// in both cases, so the behaviour is the same with and without Redis. Keys follow <see cref="CacheKeys.For"/>.
/// </summary>
public interface ICacheProvider
{
/// <summary>Whether the cache is shared between instances (Redis reachable).</summary>
bool IsDistributed { get; }
/// <summary>Reads a value.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="key">The key.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The value, or <see langword="default"/> when missing or expired.</returns>
ValueTask<T?> GetAsync<T>(string key, CancellationToken cancellationToken = default);
/// <summary>Stores a value.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <param name="timeToLive">How long it stays.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
ValueTask SetAsync<T>(string key, T value, TimeSpan timeToLive, CancellationToken cancellationToken = default);
/// <summary>Removes a value.</summary>
/// <param name="key">The key.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default);
/// <summary>Removes every key that starts with <paramref name="prefix"/>.</summary>
/// <param name="prefix">The prefix, for example <c>kh:DEMO:permissions:</c>.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
ValueTask RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default);
/// <summary>Reads a value, or creates and stores it.</summary>
/// <typeparam name="T">The value type.</typeparam>
/// <param name="key">The key.</param>
/// <param name="factory">Creates the value when missing.</param>
/// <param name="timeToLive">How long it stays.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The value.</returns>
ValueTask<T> GetOrCreateAsync<T>(string key, Func<CancellationToken, ValueTask<T>> factory, TimeSpan timeToLive, CancellationToken cancellationToken = default);
/// <summary>
/// Removes the keys with <paramref name="prefix"/> here and tells the other instances to do the same (Redis pub/sub).
/// </summary>
/// <param name="prefix">The prefix.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
ValueTask PublishInvalidationAsync(string prefix, CancellationToken cancellationToken = default);
}
/// <summary>Cache keys: <c>kh:{company}:{area}:{key}</c>, with <c>_</c> for the instance (SPEC §9).</summary>
public static class CacheKeys
{
/// <summary>The instance scope.</summary>
public const string Instance = "_";
/// <summary>A key.</summary>
/// <param name="company">The company code, or <see langword="null"/> for the instance.</param>
/// <param name="area">The area, for example <c>permissions</c>.</param>
/// <param name="key">The key inside the area.</param>
/// <returns>The full key.</returns>
public static string For(string? company, string area, string key) => Prefix(company, area) + key;
/// <summary>The prefix of an area, for <see cref="ICacheProvider.RemoveByPrefixAsync"/>.</summary>
/// <param name="company">The company code, or <see langword="null"/> for the instance.</param>
/// <param name="area">The area.</param>
/// <returns>The prefix, ending with a colon.</returns>
public static string Prefix(string? company, string area)
{
ArgumentException.ThrowIfNullOrWhiteSpace(area);
return "kh:" + (string.IsNullOrEmpty(company) ? Instance : company) + ":" + area + ":";
}
}