Skip to content

Commit e1fa74e

Browse files
authored
Feature - use default CacheConfiguration instead of allowCaching (#255)
* Feature - use default CacheConfiguration instead of �llowCaching * pr-trail: add some extra guards against invalid passed-along arguments * Update CacheConfiguration.cs * pr-sug: make the default ctor obsolete
1 parent f2eca30 commit e1fa74e

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

docs/preview/features/secret-store/provider/key-vault.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PM > Install-Package Arcus.Security.Providers.AzureKeyVault
1717
After installing the package, the addtional extensions becomes available when building the secret store.
1818

1919
```csharp
20+
using Arcus.Security.Core.Caching.Configuration;
2021
using Microsoft.Extensions.Hosting;
2122

2223
public class Program
@@ -48,7 +49,7 @@ public class Program
4849
builder.AddAzureKeyVault(vaultAuthentication, vaultConfiguration);
4950

5051
// Adding a default cached variant of the Azure Key Vault provider (default: 5 min caching).
51-
builder.AddAzureKeyVaultWithManagedIdentity(keyVaultUri, allowCaching: true);
52+
builder.AddAzureKeyVaultWithManagedIdentity(keyVaultUri, cacheConfiguration: CacheConfiguration.Default);
5253

5354
// Assing a configurable cached variant of the Azure Key Vault provider.
5455
var cacheConfiguration = new CacheConfiguration(TimeSpan.FromMinutes(1));

src/Arcus.Security.Core/Caching/CachedSecretProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public CachedSecretProvider(ISecretProvider secretProvider, ICacheConfiguration
6262
/// </summary>
6363
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="secretProvider"/> is <c>null</c>.</exception>
6464
public CachedSecretProvider(ISecretProvider secretProvider) :
65-
this(secretProvider, new CacheConfiguration(), new MemoryCache(new MemoryCacheOptions()))
65+
this(secretProvider, CacheConfiguration.Default, new MemoryCache(new MemoryCacheOptions()))
6666
{
6767
}
6868

src/Arcus.Security.Core/Caching/Configuration/CacheConfiguration.cs

+14-10
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,32 @@ namespace Arcus.Security.Core.Caching.Configuration
99
public class CacheConfiguration : ICacheConfiguration
1010
{
1111
/// <summary>
12-
/// Duration for which an entry should be cached
12+
/// Initializes a new instance of the <see cref="CacheConfiguration"/> class with default cache entry of 5 minutes.
1313
/// </summary>
14-
public TimeSpan Duration { get; }
14+
[Obsolete ("Use the " + nameof(Default) + " static property to retrieve a default caching configuration instance")]
15+
public CacheConfiguration() : this(TimeSpan.FromMinutes(5))
16+
{
17+
}
1518

1619
/// <summary>
17-
/// Constructor
20+
/// Initializes a new instance of the <see cref="CacheConfiguration"/> class.
1821
/// </summary>
1922
/// <param name="duration">Duration for which an entry should be cached</param>
2023
/// <exception cref="ArgumentOutOfRangeException">Thrown when the cache duration is not a positive time duration.</exception>
2124
public CacheConfiguration(TimeSpan duration)
2225
{
2326
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration in which the caching should take place");
24-
2527
Duration = duration;
2628
}
2729

2830
/// <summary>
29-
/// Constructor with default cache entry of 5 minutes
31+
/// Gets the default <see cref="ICacheConfiguration"/> that takes in 5 minutes as default cache duration.
3032
/// </summary>
31-
public CacheConfiguration()
32-
{
33-
Duration = TimeSpan.FromMinutes(5);
34-
}
33+
public static ICacheConfiguration Default { get; } = new CacheConfiguration(TimeSpan.FromMinutes(5));
34+
35+
/// <summary>
36+
/// Gets the duration for which an entry should be cached.
37+
/// </summary>
38+
public TimeSpan Duration { get; }
3539
}
36-
}
40+
}

src/Arcus.Security.Core/Caching/Configuration/ICacheConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Arcus.Security.Core.Caching.Configuration
88
public interface ICacheConfiguration
99
{
1010
/// <summary>
11-
/// Duration for which an entry should be cached
11+
/// Gets the duration for which an entry should be cached.
1212
/// </summary>
1313
TimeSpan Duration { get; }
1414
}

src/Arcus.Security.Core/Caching/SecretProviderCachingExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
2424
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
2525
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
2626
Guard.NotNull(memoryCache, nameof(memoryCache), "Requires a memory caching implementation to include caching while retrieving secrets");
27-
27+
2828
return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration), memoryCache);
2929
}
3030

@@ -41,7 +41,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
4141
{
4242
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
4343
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
44-
44+
4545
return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration));
4646
}
4747

@@ -55,7 +55,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
5555
public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvider)
5656
{
5757
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
58-
58+
5959
return new CachedSecretProvider(secretProvider);
6060
}
6161
}

0 commit comments

Comments
 (0)