Skip to content

Commit 9115e11

Browse files
(GH-613) Add new options builder extensions to the redis caching provider
1 parent 9ffff66 commit 9115e11

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/DotNetToolkit.Repository.Caching.Redis/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 RedisCacheProvider())
5+
.UseRedis(...)
66
.Options;
77

88
var repo = new Repository<Customer>(options);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace DotNetToolkit.Repository.Caching.Redis
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 redis.
15+
/// </summary>
16+
/// <param name="source">The repository options builder.</param>
17+
/// <returns>The same builder instance.</returns>
18+
/// <remarks>This will connect to a single server on the local machine using the default redis port (6379).</remarks>
19+
public static RepositoryOptionsBuilder UseRedis([NotNull] this RepositoryOptionsBuilder source)
20+
{
21+
Guard.NotNull(source, nameof(source));
22+
23+
source.UseCachingProvider(new RedisCacheProvider());
24+
25+
return source;
26+
}
27+
28+
/// <summary>
29+
/// Configures the caching provider to use redis.
30+
/// </summary>
31+
/// <param name="source">The repository options builder.</param>
32+
/// <param name="optionsAction">The options action.</param>
33+
/// <returns>The same builder instance.</returns>
34+
public static RepositoryOptionsBuilder UseRedis([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action<RedisCacheOptions> optionsAction)
35+
{
36+
Guard.NotNull(source, nameof(source));
37+
Guard.NotNull(optionsAction, nameof(optionsAction));
38+
39+
var options = new RedisCacheOptions();
40+
41+
optionsAction(options);
42+
43+
var provider = new RedisCacheProvider(
44+
options.Host,
45+
options.Username,
46+
options.Password,
47+
options.Ssl,
48+
options.AllowAdmin,
49+
options.DefaultDatabase,
50+
options.Expiry);
51+
52+
source.UseCachingProvider(provider);
53+
54+
return source;
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)