Skip to content

Commit a97baec

Browse files
(GH-613) Add new options buider extensions to the in-memory caching provider
1 parent 3be8099 commit a97baec

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/DotNetToolkit.Repository.Caching.InMemory/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
```csharp
44
var options = new RepositoryOptionsBuilder()
5-
.UseCachingProvider(new InMemoryCacheProvider())
5+
.UseInMemoryCache(...) // for microsoft in-memory cache
66
.Options;
77

88
var repo = new Repository<Customer>(options);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace DotNetToolkit.Repository.Caching.InMemory
2+
{
3+
using Configuration.Options;
4+
using JetBrains.Annotations;
5+
using System;
6+
using Utility;
7+
8+
/// <summary>
9+
/// Contains various extension methods for <see cref="RepositoryOptionsBuilder" />
10+
/// </summary>
11+
public static class RepositoryOptionsBuilderExtensions
12+
{
13+
/// <summary>
14+
/// Configures the caching provider to use microsoft's in-memory cache.
15+
/// </summary>
16+
/// <param name="source">The repository options builder.</param>
17+
/// <returns>The same builder instance.</returns>
18+
public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source)
19+
{
20+
Guard.NotNull(source, nameof(source));
21+
22+
source.UseCachingProvider(new InMemoryCacheProvider());
23+
24+
return source;
25+
}
26+
27+
/// <summary>
28+
/// Configures the caching provider to use the in-memory cache.
29+
/// </summary>
30+
/// <param name="source">The repository options builder.</param>
31+
/// <param name="optionsAction">The options action.</param>
32+
/// <returns>The same builder instance.</returns>
33+
public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action<InMemoryCacheOptions> optionsAction)
34+
{
35+
Guard.NotNull(source, nameof(source));
36+
Guard.NotNull(optionsAction, nameof(optionsAction));
37+
38+
var options = new InMemoryCacheOptions();
39+
40+
optionsAction(options);
41+
42+
var provider = new InMemoryCacheProvider(
43+
options.Clock,
44+
options.ExpirationScanFrequency,
45+
options.Expiry);
46+
47+
source.UseCachingProvider(provider);
48+
49+
return source;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)