Skip to content

Commit

Permalink
L2 Cache retry intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 committed Mar 4, 2021
1 parent eae9d7e commit 92fdc0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ protected override async Task RemoveKeyAsync(string cacheKey)
catch (Exception ex)
{
_logger.LogError($"[IdWebCache] Connection issue encountered with Distributed cache. Currently using In Memory cache only. Error message: {ex.Message} ");

if (_distributedCacheOptions.OnL2CacheFailure != null && _distributedCacheOptions.OnL2CacheFailure(ex))
{
_logger.LogDebug($"[IdWebCache] DistributedCache: Retry to remove cacheKey {cacheKey}. ");
await _distributedCache.RemoveAsync(cacheKey).ConfigureAwait(false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Extensions.Caching.Distributed;

namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed
Expand All @@ -18,6 +19,17 @@ public class MsalDistributedTokenCacheAdapterOptions : DistributedCacheEntryOpti
/// </summary>
public long L1CacheSizeLimit { get; set; } = 500;

/// <summary>
/// Callback offered to the app to be notified when the L2 cache fails.
/// This way the app is given the possibility to act on the L2 cache,
/// for instance, in the case of Redis, to reconnect. This is left to the application as it's
/// the only one that knows about the real implementation of the L2 cache.
/// The handler should return <c>true</c> if the cache should try again the operation, and
/// <c>false</c> otherwise. When <c>true</c> is passed and the retry fails, an exception
/// will be thrown.
/// </summary>
public Func<Exception, bool>? OnL2CacheFailure { get; set; }

/// <summary>
/// Value more than 0, less than 1, to set the In Memory (L1) cache
/// expiration time values relative to the Distributed (L2) cache.
Expand Down
13 changes: 13 additions & 0 deletions tests/WebAppCallsWebApiCallsGraph/Client/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web.UI;
using Microsoft.Identity.Web.TokenCacheProviders.Distributed;

namespace WebApp_OpenIDConnect_DotNet
{
Expand Down Expand Up @@ -48,6 +49,18 @@ public void ConfigureServices(IServiceCollection services)
options.Configuration = Configuration.GetConnectionString("Redis");
options.InstanceName = "RedisDemos_"; //should be unique to the app
});
services.Configure<MsalDistributedTokenCacheAdapterOptions>(options =>
{
options.OnL2CacheFailure = (ex) =>
{
if (ex is StackExchange.Redis.RedisConnectionException)
{
// action: try to reconnect or something
return true; //try to do the cache operation again
}
return false;
};
});
#endif

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
Expand Down

0 comments on commit 92fdc0e

Please sign in to comment.