Cache workspace feature flags in the API

C# · ASP.NET Core · intermediate · modification

The feature flag lookup is a frequent database query: most requests check one to three flags, and each check reads `workspace_features`. This PR caches each workspace's enabled features in memory for 60 seconds. `FeatureFlags` becomes a singleton (`AddFeatureFlags` now registers it with `AddSingleton`) that keeps the loaded feature sets by workspace id and reloads a workspace's set once it is a minute old, using the injected `TimeProvider`. Because `IWorkspaceFeatureStore` is scoped (it uses the `AppDbContext`), the service opens its own DI scope for each load instead of holding on to a scoped dependency. `FeatureFlagsTests.cs` (new) adds unit tests for the cache with a manual clock.

Tasklane, a project-management SaaS. ASP.NET Core 10 minimal API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), EF Core 10 with Npgsql, xUnit for tests. The API runs as three instances behind a load balancer, about 400 requests per second at peak; each instance keeps its own in-process state. Existing and unchanged: `IFeatureFlags` (one method, `Task<bool> IsEnabledAsync(int workspaceId, string feature, CancellationToken ct)`), injected and awaited by about 30 endpoints and two endpoint filters. `IWorkspaceFeatureStore` is registered as scoped and uses `AppDbContext`: `Task<IReadOnlySet<string>> GetEnabledAsync(int workspaceId, CancellationToken ct)` runs one indexed query on `workspace_features` (`WorkspaceId`, `Feature`, `Enabled`) and returns a new `HashSet<string>` (ordinal comparer) of the workspace's enabled feature keys, empty for a workspace with none; nothing modifies the set after it is returned. Feature keys are lowercase ASCII constants such as `advanced-reports` and `time-tracking`; workspace ids are positive, and every flag check runs after the existing workspace-membership filter, so `workspaceId` is always an existing workspace the signed-in user belongs to. `Program.cs` registers `TimeProvider.System` as a singleton, the `AppDbContext`, the store, and calls `builder.Services.AddFeatureFlags()`. Flags are edited in the admin console, a separate service that writes `workspace_features`. An exception from the store goes to the existing error handler, which logs it and answers 500.

Requirements

Files touched

Review this PR

C# practice