Skip to content

Commit

Permalink
Feature - use default CacheConfiguration instead of allowCaching (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
stijnmoreels authored Feb 22, 2021
1 parent f2eca30 commit e1fa74e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
3 changes: 2 additions & 1 deletion docs/preview/features/secret-store/provider/key-vault.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PM > Install-Package Arcus.Security.Providers.AzureKeyVault
After installing the package, the addtional extensions becomes available when building the secret store.

```csharp
using Arcus.Security.Core.Caching.Configuration;
using Microsoft.Extensions.Hosting;

public class Program
Expand Down Expand Up @@ -48,7 +49,7 @@ public class Program
builder.AddAzureKeyVault(vaultAuthentication, vaultConfiguration);

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

// Assing a configurable cached variant of the Azure Key Vault provider.
var cacheConfiguration = new CacheConfiguration(TimeSpan.FromMinutes(1));
Expand Down
2 changes: 1 addition & 1 deletion src/Arcus.Security.Core/Caching/CachedSecretProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CachedSecretProvider(ISecretProvider secretProvider, ICacheConfiguration
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="secretProvider"/> is <c>null</c>.</exception>
public CachedSecretProvider(ISecretProvider secretProvider) :
this(secretProvider, new CacheConfiguration(), new MemoryCache(new MemoryCacheOptions()))
this(secretProvider, CacheConfiguration.Default, new MemoryCache(new MemoryCacheOptions()))
{
}

Expand Down
24 changes: 14 additions & 10 deletions src/Arcus.Security.Core/Caching/Configuration/CacheConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@ namespace Arcus.Security.Core.Caching.Configuration
public class CacheConfiguration : ICacheConfiguration
{
/// <summary>
/// Duration for which an entry should be cached
/// Initializes a new instance of the <see cref="CacheConfiguration"/> class with default cache entry of 5 minutes.
/// </summary>
public TimeSpan Duration { get; }
[Obsolete ("Use the " + nameof(Default) + " static property to retrieve a default caching configuration instance")]
public CacheConfiguration() : this(TimeSpan.FromMinutes(5))
{
}

/// <summary>
/// Constructor
/// Initializes a new instance of the <see cref="CacheConfiguration"/> class.
/// </summary>
/// <param name="duration">Duration for which an entry should be cached</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the cache duration is not a positive time duration.</exception>
public CacheConfiguration(TimeSpan duration)
{
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration in which the caching should take place");

Duration = duration;
}

/// <summary>
/// Constructor with default cache entry of 5 minutes
/// Gets the default <see cref="ICacheConfiguration"/> that takes in 5 minutes as default cache duration.
/// </summary>
public CacheConfiguration()
{
Duration = TimeSpan.FromMinutes(5);
}
public static ICacheConfiguration Default { get; } = new CacheConfiguration(TimeSpan.FromMinutes(5));

/// <summary>
/// Gets the duration for which an entry should be cached.
/// </summary>
public TimeSpan Duration { get; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Arcus.Security.Core.Caching.Configuration
public interface ICacheConfiguration
{
/// <summary>
/// Duration for which an entry should be cached
/// Gets the duration for which an entry should be cached.
/// </summary>
TimeSpan Duration { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
Guard.NotNull(memoryCache, nameof(memoryCache), "Requires a memory caching implementation to include caching while retrieving secrets");

return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration), memoryCache);
}

Expand All @@ -41,7 +41,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");

return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration));
}

Expand All @@ -55,7 +55,7 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvider)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");

return new CachedSecretProvider(secretProvider);
}
}
Expand Down
Loading

0 comments on commit e1fa74e

Please sign in to comment.