Skip to content

Commit 49a0dfe

Browse files
(GH-665) Add json serializer settings to redis caching provider
1 parent a24fd96 commit 49a0dfe

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/DotNetToolkit.Repository.Caching.Redis/RedisCacheProvider.cs renamed to src/DotNetToolkit.Repository.Caching.Redis/Internal/RedisCacheProvider.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DotNetToolkit.Repository.Caching.Redis
1+
namespace DotNetToolkit.Repository.Caching.Redis.Internal
22
{
33
using Configuration.Caching;
44
using Newtonsoft.Json;
@@ -12,6 +12,9 @@ internal class RedisCacheProvider : ICacheProvider
1212

1313
private IDatabase _redis;
1414
private readonly Lazy<ConnectionMultiplexer> _lazyConnection;
15+
private readonly JsonSerializerSettings _serializerSettings;
16+
17+
private const string DefaultHost = "localhost";
1518

1619
#endregion
1720

@@ -25,17 +28,18 @@ internal class RedisCacheProvider : ICacheProvider
2528

2629
#region Constructors
2730

28-
public RedisCacheProvider()
29-
: this("localhost", null, null, false, false, null, null) { }
31+
public RedisCacheProvider(JsonSerializerSettings serializerSettings = null)
32+
: this(DefaultHost, null, null, false, false, null, null, serializerSettings) { }
3033

31-
public RedisCacheProvider(string host, string username, string password, bool ssl, bool allowAdmin, int? defaultDatabase, TimeSpan? expiry)
32-
: this(GetConfigurationOptions(host, username, password, ssl, allowAdmin, defaultDatabase), expiry) { }
34+
public RedisCacheProvider(string host, string username, string password, bool ssl, bool allowAdmin, int? defaultDatabase, TimeSpan? expiry, JsonSerializerSettings serializerSettings)
35+
: this(GetConfigurationOptions(host, username, password, ssl, allowAdmin, defaultDatabase), expiry, serializerSettings) { }
3336

34-
private RedisCacheProvider(ConfigurationOptions options, TimeSpan? expiry)
37+
private RedisCacheProvider(ConfigurationOptions options, TimeSpan? expiry, JsonSerializerSettings serializerSettings = null)
3538
{
3639
Guard.NotNull(options, nameof(options));
3740

3841
_lazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(options));
42+
_serializerSettings = serializerSettings;
3943

4044
Expiry = expiry;
4145
}
@@ -44,20 +48,20 @@ private RedisCacheProvider(ConfigurationOptions options, TimeSpan? expiry)
4448

4549
#region Private Methods
4650

47-
private static string Serialize(object o)
51+
private string Serialize(object o)
4852
{
4953
if (o == null)
5054
return null;
5155

52-
return JsonConvert.SerializeObject(o);
56+
return JsonConvert.SerializeObject(o, _serializerSettings);
5357
}
5458

55-
private static T Deserialize<T>(string v)
59+
private T Deserialize<T>(string v)
5660
{
5761
if (string.IsNullOrEmpty(v))
5862
return default(T);
5963

60-
return JsonConvert.DeserializeObject<T>(v);
64+
return JsonConvert.DeserializeObject<T>(v, _serializerSettings);
6165
}
6266

6367
private static ConfigurationOptions GetConfigurationOptions(string host, string username, string password, bool ssl, bool allowAdmin, int? defaultDatabase)

src/DotNetToolkit.Repository.Caching.Redis/RedisCacheOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace DotNetToolkit.Repository.Caching.Redis
22
{
33
using JetBrains.Annotations;
4+
using Newtonsoft.Json;
45
using System;
56
using Utility;
67

@@ -16,6 +17,7 @@ public class RedisCacheOptions
1617
private bool _allowAdmin;
1718
private int? _defaultDatabase;
1819
private TimeSpan? _expiry;
20+
private JsonSerializerSettings _serializerSettings;
1921

2022
/// <summary>
2123
/// Gets the host.
@@ -52,6 +54,11 @@ public class RedisCacheOptions
5254
/// </summary>
5355
public TimeSpan? Expiry { get { return _expiry; } }
5456

57+
/// <summary>
58+
/// Gets the json serializer settings.
59+
/// </summary>
60+
public JsonSerializerSettings SerializerSettings { get { return _serializerSettings; } }
61+
5562
/// <summary>
5663
/// Adds the giving password to the options.
5764
/// </summary>
@@ -144,5 +151,17 @@ public RedisCacheOptions WithExpiry([NotNull] TimeSpan expiry)
144151

145152
return this;
146153
}
154+
155+
/// <summary>
156+
/// Adds the giving json serializer settings to the options.
157+
/// </summary>
158+
/// <param name="serializerSettings">The json srialzer settings to be added.</param>
159+
/// <returns>The new options instance with the given json serializer settings added.</returns>
160+
public RedisCacheOptions WithJsonSerializerSettings([NotNull] JsonSerializerSettings serializerSettings)
161+
{
162+
_serializerSettings = Guard.NotNull(serializerSettings, nameof(serializerSettings));
163+
164+
return this;
165+
}
147166
}
148167
}

src/DotNetToolkit.Repository.Caching.Redis/RepositoryOptionsBuilderExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace DotNetToolkit.Repository.Caching.Redis
22
{
33
using Configuration.Options;
4+
using Internal;
45
using JetBrains.Annotations;
56
using System;
67
using Utility;
@@ -47,7 +48,8 @@ public static RepositoryOptionsBuilder UseRedis([NotNull] this RepositoryOptions
4748
options.Ssl,
4849
options.AllowAdmin,
4950
options.DefaultDatabase,
50-
options.Expiry);
51+
options.Expiry,
52+
options.SerializerSettings);
5153

5254
source.UseCachingProvider(provider);
5355

0 commit comments

Comments
 (0)