Skip to content

Commit 620bbf6

Browse files
(GH-665) Add json serializer settings to couchbase caching provider
1 parent 1aca764 commit 620bbf6

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/DotNetToolkit.Repository.Caching.Couchbase/CouchbaseCacheOptions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace DotNetToolkit.Repository.Caching.Couchbase
22
{
3+
using global::Couchbase.Core.Serialization;
34
using JetBrains.Annotations;
45
using System;
56
using Utility;
@@ -14,6 +15,7 @@ public class CouchbaseCacheOptions
1415
private string _username;
1516
private string _password;
1617
private TimeSpan? _expiry;
18+
private Func<ITypeSerializer> _serializer;
1719

1820
/// <summary>
1921
/// Gets the host.
@@ -40,6 +42,11 @@ public class CouchbaseCacheOptions
4042
/// </summary>
4143
public TimeSpan? Expiry { get { return _expiry; } }
4244

45+
/// <summary>
46+
/// Gets the serializer.
47+
/// </summary>
48+
public Func<ITypeSerializer> Serializer { get { return _serializer; } }
49+
4350
/// <summary>
4451
/// Adds the giving bucketname to the options.
4552
/// </summary>
@@ -90,7 +97,7 @@ public CouchbaseCacheOptions WithEndPoint([NotNull] string host, int port)
9097
/// <summary>
9198
/// Adds the giving endpoint to the options.
9299
/// </summary>
93-
/// <param name="hostAndPort">The host and port to be added.</param>
100+
/// <param name="hostAndPort">The address and the port of the server in the format 'host:port' to be added.</param>
94101
public CouchbaseCacheOptions WithEndPoint([NotNull] string hostAndPort)
95102
{
96103
_host = Guard.NotEmpty(hostAndPort, nameof(hostAndPort));
@@ -108,5 +115,17 @@ public CouchbaseCacheOptions WithExpiry([NotNull] TimeSpan expiry)
108115

109116
return this;
110117
}
118+
119+
/// <summary>
120+
/// Adds the giving json serializer settings to the options.
121+
/// </summary>
122+
/// <param name="serializer">The serializer to be added.</param>
123+
/// <returns>The new options instance with the given json serializer settings added.</returns>
124+
public CouchbaseCacheOptions WithSerializer([NotNull] Func<ITypeSerializer> serializer)
125+
{
126+
_serializer = Guard.NotNull(serializer, nameof(serializer));
127+
128+
return this;
129+
}
111130
}
112131
}

src/DotNetToolkit.Repository.Caching.Couchbase/CouchbaseCacheProvider.cs renamed to src/DotNetToolkit.Repository.Caching.Couchbase/Internal/CouchbaseCacheProvider.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
namespace DotNetToolkit.Repository.Caching.Couchbase
1+
namespace DotNetToolkit.Repository.Caching.Couchbase.Internal
22
{
33
using Configuration.Caching;
44
using global::Couchbase;
55
using global::Couchbase.Configuration.Client;
6+
using global::Couchbase.Core.Serialization;
7+
using Newtonsoft.Json;
68
using System;
79
using System.Collections.Generic;
810
using Utility;
@@ -24,8 +26,8 @@ internal class CouchbaseCacheProvider : ICacheProvider
2426

2527
#region Constructors
2628

27-
public CouchbaseCacheProvider(string host, string bucketName, string username, string password, TimeSpan? expiry)
28-
: this(GetConfigurationOptions(host, bucketName, username, password), expiry) { }
29+
public CouchbaseCacheProvider(string host, string bucketName, string username, string password, TimeSpan? expiry, Func<ITypeSerializer> serializer)
30+
: this(GetConfigurationOptions(host, bucketName, username, password, serializer), expiry) { }
2931

3032
private CouchbaseCacheProvider(ClientConfiguration config, TimeSpan? expiry)
3133
{
@@ -37,7 +39,7 @@ private CouchbaseCacheProvider(ClientConfiguration config, TimeSpan? expiry)
3739

3840
#region Private Methods
3941

40-
private static ClientConfiguration GetConfigurationOptions(string host, string username, string password, string bucketName)
42+
private static ClientConfiguration GetConfigurationOptions(string host, string username, string password, string bucketName, Func<ITypeSerializer> serializer)
4143
{
4244
var config = new ClientConfiguration();
4345
var bucket = !string.IsNullOrEmpty(bucketName) ? bucketName : DefaultBucketName;
@@ -61,6 +63,11 @@ private static ClientConfiguration GetConfigurationOptions(string host, string u
6163
}
6264
};
6365

66+
if (serializer != null)
67+
{
68+
config.Serializer = serializer;
69+
}
70+
6471
return config;
6572
}
6673

src/DotNetToolkit.Repository.Caching.Couchbase/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.Couchbase
22
{
33
using Configuration.Options;
4+
using Internal;
45
using JetBrains.Annotations;
56
using System;
67
using Utility;
@@ -30,7 +31,8 @@ public static RepositoryOptionsBuilder UseCouchbase([NotNull] this RepositoryOpt
3031
options.BucketName,
3132
options.Username,
3233
options.Password,
33-
options.Expiry);
34+
options.Expiry,
35+
options.Serializer);
3436

3537
source.UseCachingProvider(provider);
3638

0 commit comments

Comments
 (0)