fix(esg): short-TTL definition cache for central lookups (review 2026-10-21 05/P2)

This commit is contained in:
Joseph Doherty
2026-10-22 08:41:54 -04:00
parent ee8be8001e
commit 12f815a7ea
8 changed files with 193 additions and 4 deletions
@@ -26,6 +26,7 @@ public class ExternalSystemClient : IExternalSystemClient
private readonly StoreAndForwardService? _storeAndForward;
private readonly ILogger<ExternalSystemClient> _logger;
private readonly ExternalSystemGatewayOptions _options;
private readonly ExternalDefinitionLookupCache? _definitionCache;
/// <summary>
/// Shared options for the per-call AuthConfiguration JSON parse in
@@ -44,13 +45,16 @@ public class ExternalSystemClient : IExternalSystemClient
/// <param name="logger">Logger instance.</param>
/// <param name="storeAndForward">Store-and-forward service for buffering transient failures, or null if not available.</param>
/// <param name="options">Configuration options, or null for defaults.</param>
/// <param name="definitionCache">Optional short-TTL definition cache; null (or a zero TTL) resolves every call from the repository.</param>
public ExternalSystemClient(
IHttpClientFactory httpClientFactory,
IExternalSystemRepository repository,
ILogger<ExternalSystemClient> logger,
StoreAndForwardService? storeAndForward = null,
IOptions<ExternalSystemGatewayOptions>? options = null)
IOptions<ExternalSystemGatewayOptions>? options = null,
ExternalDefinitionLookupCache? definitionCache = null)
{
_definitionCache = definitionCache;
_httpClientFactory = httpClientFactory;
_repository = repository;
_logger = logger;
@@ -815,11 +819,17 @@ public class ExternalSystemClient : IExternalSystemClient
// fetch-all-then-filter — definitions are resolved on every hot-path call
// (a script's ExternalSystem.Call()), so the repository performs an indexed
// query rather than loading every system / every method into memory.
var system = await _repository.GetExternalSystemByNameAsync(systemName, cancellationToken);
var system = _definitionCache is { Enabled: true }
? await _definitionCache.GetSystemAsync(systemName,
() => _repository.GetExternalSystemByNameAsync(systemName, cancellationToken))
: await _repository.GetExternalSystemByNameAsync(systemName, cancellationToken);
if (system == null)
return (null, null);
var method = await _repository.GetMethodByNameAsync(system.Id, methodName, cancellationToken);
var method = _definitionCache is { Enabled: true }
? await _definitionCache.GetMethodAsync(system.Id, methodName,
() => _repository.GetMethodByNameAsync(system.Id, methodName, cancellationToken))
: await _repository.GetMethodByNameAsync(system.Id, methodName, cancellationToken);
return (system, method);
}