@@ -9,8 +9,18 @@ namespace StackExchange.Redis
99 public readonly struct RedisChannel : IEquatable < RedisChannel >
1010 {
1111 internal readonly byte [ ] ? Value ;
12- internal readonly bool _isPatternBased ;
13- internal readonly bool _isSharded ;
12+
13+ internal readonly RedisChannelOptions Options ;
14+
15+ [ Flags ]
16+ internal enum RedisChannelOptions
17+ {
18+ None = 0 ,
19+ Pattern = 1 << 0 ,
20+ Sharded = 1 << 1 ,
21+ }
22+
23+ internal RedisCommand PublishCommand => IsSharded ? RedisCommand . SPUBLISH : RedisCommand . PUBLISH ;
1424
1525 /// <summary>
1626 /// Indicates whether the channel-name is either null or a zero-length value.
@@ -20,7 +30,12 @@ namespace StackExchange.Redis
2030 /// <summary>
2131 /// Indicates whether this channel represents a wildcard pattern (see <c>PSUBSCRIBE</c>).
2232 /// </summary>
23- public bool IsPattern => _isPatternBased ;
33+ public bool IsPattern => ( Options & RedisChannelOptions . Pattern ) != 0 ;
34+
35+ /// <summary>
36+ /// Indicates whether this channel represents a shard channel (see <c>SSUBSCRIBE</c>)
37+ /// </summary>
38+ public bool IsSharded => ( Options & RedisChannelOptions . Sharded ) != 0 ;
2439
2540 /// <summary>
2641 /// Indicates whether this channel represents a shard channel (see <c>SSUBSCRIBE</c>)
@@ -65,34 +80,31 @@ public static bool UseImplicitAutoPattern
6580 /// </summary>
6681 /// <param name="value">The name of the channel to create.</param>
6782 /// <param name="mode">The mode for name matching.</param>
68- public RedisChannel ( byte [ ] ? value , PatternMode mode ) : this ( value , DeterminePatternBased ( value , mode ) , false ) { }
83+ public RedisChannel ( byte [ ] ? value , PatternMode mode ) : this ( value , DeterminePatternBased ( value , mode ) ? RedisChannelOptions . Pattern : RedisChannelOptions . None ) { }
6984
7085 /// <summary>
7186 /// Create a new redis channel from a string, explicitly controlling the pattern mode.
7287 /// </summary>
7388 /// <param name="value">The string name of the channel to create.</param>
7489 /// <param name="mode">The mode for name matching.</param>
75- public RedisChannel ( string value , PatternMode mode ) : this ( value == null ? null : Encoding . UTF8 . GetBytes ( value ) , mode ) { }
90+ public RedisChannel ( string value , PatternMode mode ) : this ( value is null ? null : Encoding . UTF8 . GetBytes ( value ) , mode ) { }
7691
7792 /// <summary>
78- /// Create a new redis channel from a buffer, explicitly controlling the sharding mode .
93+ /// Create a new redis channel from a buffer, representing a sharded channel .
7994 /// </summary>
8095 /// <param name="value">The name of the channel to create.</param>
81- /// <param name="isSharded">Whether the channel is sharded.</param>
82- public RedisChannel ( byte [ ] ? value , bool isSharded ) : this ( value , false , isSharded ) { }
96+ public static RedisChannel Sharded ( byte [ ] ? value ) => new ( value , RedisChannelOptions . Sharded ) ;
8397
8498 /// <summary>
85- /// Create a new redis channel from a string, explicitly controlling the sharding mode .
99+ /// Create a new redis channel from a string, representing a sharded channel .
86100 /// </summary>
87101 /// <param name="value">The string name of the channel to create.</param>
88- /// <param name="isSharded">Whether the channel is sharded.</param>
89- public RedisChannel ( string value , bool isSharded ) : this ( value == null ? null : Encoding . UTF8 . GetBytes ( value ) , isSharded ) { }
102+ public static RedisChannel Sharded ( string value ) => new ( value is null ? null : Encoding . UTF8 . GetBytes ( value ) , RedisChannelOptions . Sharded ) ;
90103
91- private RedisChannel ( byte [ ] ? value , bool isPatternBased , bool isSharded )
104+ internal RedisChannel ( byte [ ] ? value , RedisChannelOptions options )
92105 {
93106 Value = value ;
94- _isPatternBased = isPatternBased ;
95- _isSharded = isSharded ;
107+ Options = options ;
96108 }
97109
98110 private static bool DeterminePatternBased ( byte [ ] ? value , PatternMode mode ) => mode switch
@@ -144,7 +156,7 @@ private RedisChannel(byte[]? value, bool isPatternBased, bool isSharded)
144156 /// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
145157 /// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
146158 public static bool operator == ( RedisChannel x , RedisChannel y ) =>
147- x . _isPatternBased == y . _isPatternBased && RedisValue . Equals ( x . Value , y . Value ) && x . _isSharded == y . _isSharded ;
159+ x . Options == y . Options && RedisValue . Equals ( x . Value , y . Value ) ;
148160
149161 /// <summary>
150162 /// Indicate whether two channel names are equal.
@@ -192,10 +204,10 @@ private RedisChannel(byte[]? value, bool isPatternBased, bool isSharded)
192204 /// Indicate whether two channel names are equal.
193205 /// </summary>
194206 /// <param name="other">The <see cref="RedisChannel"/> to compare to.</param>
195- public bool Equals ( RedisChannel other ) => _isPatternBased == other . _isPatternBased && RedisValue . Equals ( Value , other . Value ) && _isSharded == other . _isSharded ;
207+ public bool Equals ( RedisChannel other ) => Options == other . Options && RedisValue . Equals ( Value , other . Value ) ;
196208
197209 /// <inheritdoc/>
198- public override int GetHashCode ( ) => RedisValue . GetHashCode ( Value ) + ( _isPatternBased ? 1 : 0 ) + ( _isSharded ? 2 : 0 ) ;
210+ public override int GetHashCode ( ) => RedisValue . GetHashCode ( Value ) ^ ( int ) Options ;
199211
200212 /// <summary>
201213 /// Obtains a string representation of the channel name.
@@ -224,7 +236,7 @@ internal RedisChannel Clone()
224236 return this ;
225237 }
226238 var copy = ( byte [ ] ) Value . Clone ( ) ; // defensive array copy
227- return new RedisChannel ( copy , _isPatternBased ) ;
239+ return new RedisChannel ( copy , Options ) ;
228240 }
229241
230242 /// <summary>
0 commit comments