-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-613) Add new options buider extensions to the in-memory caching p…
…rovider
- Loading branch information
1 parent
08bdf56
commit d7d2306
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/DotNetToolkit.Repository.Caching.InMemory/RepositoryOptionsBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
namespace DotNetToolkit.Repository.Caching.InMemory | ||
{ | ||
using Configuration.Options; | ||
using JetBrains.Annotations; | ||
using System; | ||
using Utility; | ||
|
||
/// <summary> | ||
/// Contains various extension methods for <see cref="RepositoryOptionsBuilder" /> | ||
/// </summary> | ||
public static class RepositoryOptionsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the caching provider to use microsoft's in-memory cache. | ||
/// </summary> | ||
/// <param name="source">The repository options builder.</param> | ||
/// <returns>The same builder instance.</returns> | ||
public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source) | ||
{ | ||
Guard.NotNull(source, nameof(source)); | ||
|
||
source.UseCachingProvider(new InMemoryCacheProvider()); | ||
|
||
return source; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the caching provider to use the in-memory cache. | ||
/// </summary> | ||
/// <param name="source">The repository options builder.</param> | ||
/// <param name="optionsAction">The options action.</param> | ||
/// <returns>The same builder instance.</returns> | ||
public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action<InMemoryCacheOptions> optionsAction) | ||
{ | ||
Guard.NotNull(source, nameof(source)); | ||
Guard.NotNull(optionsAction, nameof(optionsAction)); | ||
|
||
var options = new InMemoryCacheOptions(); | ||
|
||
optionsAction(options); | ||
|
||
var provider = new InMemoryCacheProvider( | ||
options.Clock, | ||
options.ExpirationScanFrequency, | ||
options.Expiry); | ||
|
||
source.UseCachingProvider(provider); | ||
|
||
return source; | ||
} | ||
} | ||
} |