Skip to content

Commit f609eee

Browse files
authored
add memory cache options (#640)
* add memory cache options * add a small test
1 parent 6bbc143 commit f609eee

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Identity.Web/TokenCacheProviders/InMemory/MsalMemoryTokenCacheProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ protected override Task<byte[]> ReadCacheBytesAsync(string cacheKey)
7474
/// <returns>A <see cref="Task"/> that completes when a write operation has completed.</returns>
7575
protected override Task WriteCacheBytesAsync(string cacheKey, byte[] bytes)
7676
{
77-
_memoryCache.Set(cacheKey, bytes, _cacheOptions.AbsoluteExpirationRelativeToNow);
77+
MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions()
78+
{
79+
AbsoluteExpirationRelativeToNow = _cacheOptions.AbsoluteExpirationRelativeToNow,
80+
Size = bytes?.Length,
81+
};
82+
83+
_memoryCache.Set(cacheKey, bytes, memoryCacheEntryOptions);
7884
return Task.CompletedTask;
7985
}
8086
}

src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityAppCallingWebApiAuthenticationBuilder.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Caching.Memory;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -32,16 +33,26 @@ internal MicrosoftIdentityAppCallsWebApiAuthenticationBuilder(
3233
/// Add in memory token caches.
3334
/// </summary>
3435
/// <param name="configureOptions"><see cref="MsalMemoryTokenCacheOptions"/> to configure.</param>
36+
/// <param name="memoryCacheOptions"><see cref="MemoryCacheOptions"/> to configure.</param>
3537
/// <returns>the service collection.</returns>
3638
public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddInMemoryTokenCaches(
37-
Action<MsalMemoryTokenCacheOptions>? configureOptions = null)
39+
Action<MsalMemoryTokenCacheOptions>? configureOptions = null,
40+
Action<MemoryCacheOptions>? memoryCacheOptions = null)
3841
{
3942
if (configureOptions != null)
4043
{
4144
Services.Configure(configureOptions);
4245
}
4346

44-
Services.AddMemoryCache();
47+
if (memoryCacheOptions != null)
48+
{
49+
Services.AddMemoryCache(memoryCacheOptions);
50+
}
51+
else
52+
{
53+
Services.AddMemoryCache();
54+
}
55+
4556
Services.AddHttpContextAccessor();
4657
Services.AddSingleton<IMsalTokenCacheProvider, MsalMemoryTokenCacheProvider>();
4758
return this;

tests/Microsoft.Identity.Web.Test/WebAppExtensionsTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,13 @@ public void AddMicrosoftIdentityWebApp_AddsInMemoryTokenCaches()
388388
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
389389
.AddMicrosoftIdentityWebApp(_configureMsOptions)
390390
.EnableTokenAcquisitionToCallDownstreamApi(_configureAppOptions)
391-
.AddInMemoryTokenCaches(options => options.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1));
391+
.AddInMemoryTokenCaches(
392+
options => options.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1),
393+
memoryCacheOptions: options => { options.SizeLimit = (long)1e9; });
392394

393395
// Assert correct services added
394396
Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions<MsalMemoryTokenCacheOptions>));
397+
Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions<MemoryCacheOptions>));
395398
Assert.Contains(services, s => s.ServiceType == typeof(IMemoryCache));
396399
Assert.Contains(services, s => s.ServiceType == typeof(IMsalTokenCacheProvider));
397400
}

0 commit comments

Comments
 (0)