Skip to content

Commit

Permalink
chore: Log in memory cache value for authorized parties (#1526)
Browse files Browse the repository at this point in the history
Co-authored-by: Knut Haug <knut.espen.haug@digdir.no>
Co-authored-by: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 17abb10 commit 40a1add
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities;
using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions;
using Digdir.Domain.Dialogporten.Infrastructure.Common.Exceptions;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;

Expand All @@ -22,6 +23,7 @@ internal sealed class AltinnAuthorizationClient : IAltinnAuthorization
private readonly IFusionCache _partiesCache;
private readonly IUser _user;
private readonly ILogger _logger;
private readonly IMemoryCache _inMemoryCache;

private static readonly JsonSerializerOptions SerializerOptions = new()
{
Expand All @@ -33,13 +35,15 @@ public AltinnAuthorizationClient(
HttpClient client,
IFusionCacheProvider cacheProvider,
IUser user,
ILogger<AltinnAuthorizationClient> logger)
ILogger<AltinnAuthorizationClient> logger,
IMemoryCache inMemoryCache)
{
_httpClient = client ?? throw new ArgumentNullException(nameof(client));
_pdpCache = cacheProvider.GetCache(nameof(Authorization)) ?? throw new ArgumentNullException(nameof(cacheProvider));
_partiesCache = cacheProvider.GetCache(nameof(AuthorizedPartiesResult)) ?? throw new ArgumentNullException(nameof(cacheProvider));
_user = user ?? throw new ArgumentNullException(nameof(user));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_inMemoryCache = inMemoryCache;
}

public async Task<DialogDetailsAuthorizationResult> GetDialogDetailsAuthorization(
Expand Down Expand Up @@ -82,9 +86,14 @@ public async Task<AuthorizedPartiesResult> GetAuthorizedParties(IPartyIdentifier
{
var authorizedPartiesRequest = new AuthorizedPartiesRequest(authenticatedParty);

var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token
var cacheKey = authorizedPartiesRequest.GenerateCacheKey();
var authorizedParties = await _partiesCache.GetOrSetAsync(cacheKey, async token
=> await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken);

var inMemoryCacheValue = _inMemoryCache.TryGetValue<AuthorizedPartiesResult>(cacheKey, out var inMemoryCacheEntry);
_logger.LogInformation("In memory cache value for {CacheKey}, success: {InMemoryCacheValue} value: {@InMemoryCacheEntry}",
cacheKey, inMemoryCacheValue, inMemoryCacheEntry);

// Temporary logging to debug missing authorized sub parties
_logger.LogInformation("Authorized parties for {Party}: {@AuthorizedParties}", authenticatedParty, authorizedParties);

Expand Down

0 comments on commit 40a1add

Please sign in to comment.