Skip to content

Commit

Permalink
Reenable party cache, log party name look failure, negative cache ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
elsand committed Nov 5, 2024
1 parent 493d247 commit 64a86dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ public async Task<AuthorizedPartiesResult> GetAuthorizedParties(IPartyIdentifier
CancellationToken cancellationToken = default)
{
var authorizedPartiesRequest = new AuthorizedPartiesRequest(authenticatedParty);
// var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token
// => await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);
var authorizedParties = await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, cancellationToken);
var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token
=> await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);
return flatten ? GetFlattenedAuthorizedParties(authorizedParties) : authorizedParties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Digdir.Domain.Dialogporten.Application.Externals;
using Digdir.Domain.Dialogporten.Domain.Parties;
using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;

namespace Digdir.Domain.Dialogporten.Infrastructure.Altinn.NameRegistry;
Expand All @@ -12,24 +13,36 @@ internal sealed class PartyNameRegistryClient : IPartyNameRegistry
{
private readonly IFusionCache _cache;
private readonly HttpClient _client;
private readonly ILogger<PartyNameRegistryClient> _logger;

private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};

public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider)
public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider, ILogger<PartyNameRegistryClient> logger)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_cache = cacheProvider.GetCache(nameof(NameRegistry)) ?? throw new ArgumentNullException(nameof(cacheProvider));
}

public async Task<string?> GetName(string externalIdWithPrefix, CancellationToken cancellationToken)
{
return await _cache.GetOrSetAsync(
return await _cache.GetOrSetAsync<string?>(
$"Name_{externalIdWithPrefix}",
ct => GetNameFromRegister(externalIdWithPrefix, ct),
async (ctx, ct) =>
{
var name = await GetNameFromRegister(externalIdWithPrefix, ct);
if (name is null)
{
// Short negative cache
ctx.Options.Duration = TimeSpan.FromSeconds(10);
}

return name;
},
token: cancellationToken);
}

Expand All @@ -48,7 +61,14 @@ public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProv
serializerOptions: SerializerOptions,
cancellationToken: cancellationToken);

return nameLookupResult.PartyNames.FirstOrDefault()?.Name;
var name = nameLookupResult.PartyNames.FirstOrDefault()?.Name;
if (name is null)
{
// This is PII, but this is an error condition (probably due to missing Altinn profile)
_logger.LogError("Failed to get name from party name registry for external id {ExternalId}", externalIdWithPrefix);
}

return name;
}

private static bool TryParse(string externalIdWithPrefix, [NotNullWhen(true)] out NameLookup? nameLookup)
Expand Down

0 comments on commit 64a86dd

Please sign in to comment.