< Summary

Information
Class: ArturRios.Util.WebApi.Security.Providers.CachedAuthenticationProvider
Assembly: ArturRios.Util.WebApi
File(s): D:\Repositories\dotnet-webapi-util\src\Security\Providers\CachedAuthenticationProvider.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 58
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage
100%
Covered methods: 3
Fully covered methods: 0
Total methods: 3
Method coverage: 100%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
GetAuthenticatedUserById(...)100%66100%
GetAuthenticatedUserByEmail(...)83.33%66100%

File(s)

D:\Repositories\dotnet-webapi-util\src\Security\Providers\CachedAuthenticationProvider.cs

#LineLine coverage
 1using ArturRios.Util.WebApi.Security.Interfaces;
 2using ArturRios.Util.WebApi.Security.Records;
 3using Microsoft.Extensions.Caching.Memory;
 4
 5namespace ArturRios.Util.WebApi.Security.Providers;
 6
 7/// <summary>
 8/// An <see cref="IAuthenticationProvider"/> decorator that caches resolved users in an
 9/// <see cref="IMemoryCache"/>, so repeated lookups of the same user within the configured
 10/// time-to-live are served from memory instead of the underlying store.
 11/// </summary>
 812public class CachedAuthenticationProvider(
 813    IAuthenticationProvider inner,
 814    IMemoryCache cache,
 815    CachedAuthenticationProviderOptions? options = null) : IAuthenticationProvider
 16{
 817    private readonly CachedAuthenticationProviderOptions _options = options ?? new CachedAuthenticationProviderOptions()
 18
 19    /// <inheritdoc />
 20    public AuthenticatedUser? GetAuthenticatedUserById(int id)
 1121    {
 1122        var key = $"{_options.CacheKeyPrefix}{id}";
 23
 1124        if (cache.TryGetValue(key, out AuthenticatedUser? cachedUser))
 325        {
 326            return cachedUser;
 27        }
 28
 829        var user = inner.GetAuthenticatedUserById(id);
 30
 831        if (user is not null || _options.CacheMisses)
 632        {
 633            cache.Set(key, user, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = _options.Ttl });
 634        }
 35
 836        return user;
 1137    }
 38
 39    /// <inheritdoc />
 40    public AuthenticatedUser? GetAuthenticatedUserByEmail(string email)
 541    {
 542        var key = $"{_options.EmailCacheKeyPrefix}{email}";
 43
 544        if (cache.TryGetValue(key, out AuthenticatedUser? cachedUser))
 245        {
 246            return cachedUser;
 47        }
 48
 349        var user = inner.GetAuthenticatedUserByEmail(email);
 50
 351        if (user is not null || _options.CacheMisses)
 352        {
 353            cache.Set(key, user, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = _options.Ttl });
 354        }
 55
 356        return user;
 557    }
 58}