11using System . Collections . Concurrent ;
2+
23using Cysharp . Runtime . Multicast . Remoting ;
34
45using MessagePack ;
56using MessagePack . Formatters ;
67using MessagePack . Resolvers ;
78
89using Microsoft . Extensions . Options ;
10+
911using StackExchange . Redis ;
1012
1113namespace Cysharp . Runtime . Multicast . Distributed . Redis ;
1214
15+ /// <summary>
16+ /// Provides functionality for managing multicast groups using Redis as the underlying communication mechanism.
17+ /// </summary>
18+ /// <remarks>This class allows the creation and management of asynchronous and synchronous multicast groups, where
19+ /// messages can be sent to multiple receivers. It uses Redis channels for communication and supports custom
20+ /// serialization and deserialization of keys and messages.</remarks>
1321public class RedisGroupProvider : IMulticastGroupProvider , IDisposable
1422{
1523 private readonly ConcurrentDictionary < ( string Name , Type KeyType , Type ReceiverType ) , object > _groups = new ( ) ;
@@ -19,11 +27,32 @@ public class RedisGroupProvider : IMulticastGroupProvider, IDisposable
1927 private readonly ISubscriber _subscriber ;
2028 private readonly string ? _prefix ;
2129 private readonly MessagePackSerializerOptions _messagePackSerializerOptionsForKey ;
30+ private readonly Func < string , RedisChannel > _channelFactory ;
2231
32+ /// <summary>
33+ /// Initializes a new instance of the <see cref="RedisGroupProvider"/> class with the specified proxy factory,
34+ /// serializer, and configuration options.
35+ /// </summary>
36+ /// <param name="proxyFactory">The factory used to create remote proxies for interacting with Redis.</param>
37+ /// <param name="serializer">The serializer used to serialize and deserialize data for Redis operations.</param>
38+ /// <param name="options">The configuration options for the Redis group provider.</param>
2339 public RedisGroupProvider ( IRemoteProxyFactory proxyFactory , IRemoteSerializer serializer , IOptions < RedisGroupOptions > options )
2440 : this ( proxyFactory , serializer , options . Value )
2541 { }
2642
43+ /// <summary>
44+ /// Initializes a new instance of the <see cref="RedisGroupProvider"/> class, which provides functionality for
45+ /// managing Redis-based group communication using a specified remote proxy factory, serializer, and configuration
46+ /// options.
47+ /// </summary>
48+ /// <remarks>If the <see cref="RedisGroupOptions.ConnectionMultiplexer"/> property in <paramref
49+ /// name="options"/> is <see langword="null"/>, a new connection multiplexer is created using the connection string
50+ /// specified in <see cref="RedisGroupOptions.ConnectionString"/>. Otherwise, the provided connection multiplexer is
51+ /// used.</remarks>
52+ /// <param name="proxyFactory">The factory used to create remote proxies for communication. This parameter cannot be <see langword="null"/>.</param>
53+ /// <param name="serializer">The serializer used to serialize and deserialize messages. This parameter cannot be <see langword="null"/>.</param>
54+ /// <param name="options">The configuration options for the Redis group provider, including connection settings, channel factory, and
55+ /// serialization options. This parameter cannot be <see langword="null"/>.</param>
2756 public RedisGroupProvider ( IRemoteProxyFactory proxyFactory , IRemoteSerializer serializer , RedisGroupOptions options )
2857 {
2958 _proxyFactory = proxyFactory ;
@@ -39,52 +68,33 @@ public RedisGroupProvider(IRemoteProxyFactory proxyFactory, IRemoteSerializer se
3968 _subscriber = options . ConnectionMultiplexer . GetSubscriber ( ) ;
4069 }
4170 _prefix = options . Prefix ;
71+ _channelFactory = options . ChannelFactory ;
4272
4373 var messagePackSerializerOptions = ( options . MessagePackSerializerOptionsForKey ?? MessagePackSerializer . DefaultOptions ) ;
4474 _messagePackSerializerOptionsForKey = messagePackSerializerOptions . WithResolver (
4575 CompositeResolver . Create ( [ NativeGuidFormatter . Instance ] , [ messagePackSerializerOptions . Resolver ] )
4676 ) ;
4777 }
4878
79+ /// <inheritdoc />
4980 public IMulticastAsyncGroup < TKey , TReceiver > GetOrAddGroup < TKey , TReceiver > ( string name )
5081 where TKey : IEquatable < TKey >
51- => ( IMulticastAsyncGroup < TKey , TReceiver > ) _groups . GetOrAdd ( ( name , typeof ( TKey ) , typeof ( TReceiver ) ) , _ => new RedisGroup < TKey , TReceiver > ( _prefix + name , _subscriber , _proxyFactory , _serializer , _messagePackSerializerOptionsForKey , Remove ) ) ;
82+ => ( IMulticastAsyncGroup < TKey , TReceiver > ) _groups . GetOrAdd ( ( name , typeof ( TKey ) , typeof ( TReceiver ) ) , _ => new RedisGroup < TKey , TReceiver > ( _prefix + name , _channelFactory , _subscriber , _proxyFactory , _serializer , _messagePackSerializerOptionsForKey , Remove ) ) ;
5283
84+ /// <inheritdoc />
5385 public IMulticastSyncGroup < TKey , TReceiver > GetOrAddSynchronousGroup < TKey , TReceiver > ( string name )
5486 where TKey : IEquatable < TKey >
55- => ( IMulticastSyncGroup < TKey , TReceiver > ) _groups . GetOrAdd ( ( name , typeof ( TKey ) , typeof ( TReceiver ) ) , _ => new RedisGroup < TKey , TReceiver > ( _prefix + name , _subscriber , _proxyFactory , _serializer , _messagePackSerializerOptionsForKey , Remove ) ) ;
87+ => ( IMulticastSyncGroup < TKey , TReceiver > ) _groups . GetOrAdd ( ( name , typeof ( TKey ) , typeof ( TReceiver ) ) , _ => new RedisGroup < TKey , TReceiver > ( _prefix + name , _channelFactory , _subscriber , _proxyFactory , _serializer , _messagePackSerializerOptionsForKey , Remove ) ) ;
5688
5789 private void Remove < TKey , TReceiver > ( RedisGroup < TKey , TReceiver > group )
5890 where TKey : IEquatable < TKey >
5991 {
6092 _groups . TryRemove ( ( group . Name , typeof ( TKey ) , typeof ( TReceiver ) ) , out _ ) ;
6193 }
6294
95+ /// <inheritdoc />
6396 public void Dispose ( )
6497 {
6598 _createdConnectionMultiplexer ? . Dispose ( ) ;
6699 }
67100}
68-
69- public class RedisGroupOptions
70- {
71- /// <summary>
72- /// Gets or sets the connection string to connect to Redis. If <see cref="ConnectionMultiplexer"/> property is not set, this will be used.
73- /// </summary>
74- public string ConnectionString { get ; set ; } = "localhost:6379" ;
75-
76- /// <summary>
77- /// Gets or sets a ConnectionMultiplexer instance to connect to Redis. If this is set, <see cref="ConnectionString"/> property will be ignored.
78- /// </summary>
79- public ConnectionMultiplexer ? ConnectionMultiplexer { get ; set ; }
80-
81- /// <summary>
82- /// Gets or sets a prefix for the Redis key.
83- /// </summary>
84- public string ? Prefix { get ; set ; }
85-
86- /// <summary>
87- /// Gets or sets a MessagePackSerializerOptions used for serializing the key.
88- /// </summary>
89- public MessagePackSerializerOptions ? MessagePackSerializerOptionsForKey { get ; set ; }
90- }
0 commit comments