Skip to content

Commit

Permalink
(GH-613) Add new options buider extensions to the in-memory caching p…
Browse files Browse the repository at this point in the history
…rovider
  • Loading branch information
johelvisguzman committed Oct 18, 2021
1 parent 4c2d6fc commit e783f31
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DotNetToolkit.Repository.Caching.InMemory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```csharp
var options = new RepositoryOptionsBuilder()
.UseCachingProvider(new InMemoryCacheProvider())
.UseInMemoryCache(...) // for microsoft in-memory cache
.Options;

var repo = new Repository<Customer>(options);
Expand Down
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;
}
}
}

0 comments on commit e783f31

Please sign in to comment.