22using NRedisStack . RedisStackCommands ;
33using StackExchange . Redis ;
44
5- namespace NRedisStack ;
6-
7- public static class Auxiliary
5+ namespace NRedisStack
86{
9- private static string ? _libraryName = $ "NRedisStack(.NET_v{ Environment . Version } )";
10- private static bool _setInfo = true ;
11- public static void ResetInfoDefaults ( )
12- {
13- _setInfo = true ;
14- _libraryName = $ "NRedisStack(.NET_v{ Environment . Version } )";
15- }
16- public static List < object > MergeArgs ( RedisKey key , params RedisValue [ ] items )
7+ public static class Auxiliary
178 {
18- var args = new List < object > ( items . Length + 1 ) { key } ;
19- args . AddRange ( items . Cast < object > ( ) ) ;
20- return args ;
21- }
9+ private static string ? _libraryName = $ "NRedisStack(.NET_v{ Environment . Version } )";
10+ private static bool _setInfo = true ;
11+ public static void ResetInfoDefaults ( )
12+ {
13+ _setInfo = true ;
14+ _libraryName = $ "NRedisStack(.NET_v{ Environment . Version } )";
15+ }
16+ public static List < object > MergeArgs ( RedisKey key , params RedisValue [ ] items )
17+ {
18+ var args = new List < object > ( items . Length + 1 ) { key } ;
19+ args . AddRange ( items . Cast < object > ( ) ) ;
20+ return args ;
21+ }
2222
23- public static object [ ] AssembleNonNullArguments ( params object ? [ ] arguments )
24- {
25- var args = new List < object > ( ) ;
26- foreach ( var arg in arguments )
23+ public static object [ ] AssembleNonNullArguments ( params object ? [ ] arguments )
2724 {
28- if ( arg != null )
25+ var args = new List < object > ( ) ;
26+ foreach ( var arg in arguments )
2927 {
30- args . Add ( arg ) ;
28+ if ( arg != null )
29+ {
30+ args . Add ( arg ) ;
31+ }
3132 }
32- }
3333
34- return args . ToArray ( ) ;
35- }
34+ return args . ToArray ( ) ;
35+ }
3636
37- // TODO: add all the signatures of GetDatabase
38- public static IDatabase GetDatabase ( this ConnectionMultiplexer redis ,
39- string ? LibraryName )
40- {
41- var _db = redis . GetDatabase ( ) ;
42- if ( LibraryName == null ) // the user wants to disable the library name and version sending
43- _setInfo = false ;
37+ // TODO: add all the signatures of GetDatabase
38+ public static IDatabase GetDatabase ( this ConnectionMultiplexer redis ,
39+ string ? LibraryName )
40+ {
41+ var _db = redis . GetDatabase ( ) ;
42+ if ( LibraryName == null ) // the user wants to disable the library name and version sending
43+ _setInfo = false ;
4444
45- else // the user set his own the library name
46- _libraryName = $ "NRedisStack({ LibraryName } ;.NET_v{ Environment . Version } )";
45+ else // the user set his own the library name
46+ _libraryName = $ "NRedisStack({ LibraryName } ;.NET_v{ Environment . Version } )";
4747
48- return _db ;
49- }
48+ return _db ;
49+ }
5050
51- internal static void SetInfoInPipeline ( this IDatabase db )
52- {
53- if ( _setInfo )
51+ internal static void SetInfoInPipeline ( this IDatabase db )
5452 {
55- _setInfo = false ;
56- if ( _libraryName == null ) return ;
57- Pipeline pipeline = new ( db ) ;
58- _ = pipeline . Db . ClientSetInfoAsync ( SetInfoAttr . LibraryName , _libraryName ) ;
59- _ = pipeline . Db . ClientSetInfoAsync ( SetInfoAttr . LibraryVersion , GetNRedisStackVersion ( ) ) ;
60- pipeline . Execute ( ) ;
53+ if ( _setInfo )
54+ {
55+ _setInfo = false ;
56+ if ( _libraryName == null ) return ;
57+ Pipeline pipeline = new ( db ) ;
58+ _ = pipeline . Db . ClientSetInfoAsync ( SetInfoAttr . LibraryName , _libraryName ) ;
59+ _ = pipeline . Db . ClientSetInfoAsync ( SetInfoAttr . LibraryVersion , GetNRedisStackVersion ( ) ) ;
60+ pipeline . Execute ( ) ;
61+ }
6162 }
62- }
6363
64- public static RedisResult Execute ( this IDatabase db , SerializedCommand command )
65- {
66- db . SetInfoInPipeline ( ) ;
67- return db . Execute ( command . Command , command . Args ) ;
68- }
69-
70- public static async Task < RedisResult > ExecuteAsync ( this IDatabaseAsync db , SerializedCommand command )
71- {
72- ( ( IDatabase ) db ) . SetInfoInPipeline ( ) ;
73- return await db . ExecuteAsync ( command . Command , command . Args ) ;
74- }
64+ public static RedisResult Execute ( this IDatabase db , SerializedCommand command )
65+ {
66+ db . SetInfoInPipeline ( ) ;
67+ return db . Execute ( command . Command , command . Args ) ;
68+ }
7569
76- public static List < RedisResult > ExecuteBroadcast ( this IDatabase db , string command )
77- => db . ExecuteBroadcast ( new SerializedCommand ( command ) ) ;
70+ public static async Task < RedisResult > ExecuteAsync ( this IDatabaseAsync db , SerializedCommand command )
71+ {
72+ ( ( IDatabase ) db ) . SetInfoInPipeline ( ) ;
73+ return await db . ExecuteAsync ( command . Command , command . Args ) ;
74+ }
7875
79- public static List < RedisResult > ExecuteBroadcast ( this IDatabase db , SerializedCommand command )
80- {
81- var redis = db . Multiplexer ;
82- var endpoints = redis . GetEndPoints ( ) ;
83- var results = new List < RedisResult > ( endpoints . Length ) ;
76+ public static List < RedisResult > ExecuteBroadcast ( this IDatabase db , string command )
77+ => db . ExecuteBroadcast ( new SerializedCommand ( command ) ) ;
8478
85- foreach ( var endPoint in endpoints )
79+ public static List < RedisResult > ExecuteBroadcast ( this IDatabase db , SerializedCommand command )
8680 {
87- var server = redis . GetServer ( endPoint ) ;
81+ var redis = db . Multiplexer ;
82+ var endpoints = redis . GetEndPoints ( ) ;
83+ var results = new List < RedisResult > ( endpoints . Length ) ;
8884
89- if ( server . IsReplica )
85+ foreach ( var endPoint in endpoints )
9086 {
91- continue ; // Skip replica nodes
92- }
93- // Send your command to the master node
87+ var server = redis . GetServer ( endPoint ) ;
9488
95- results . Add ( server . Multiplexer . GetDatabase ( ) . Execute ( command ) ) ;
96- }
97- return results ;
98- }
89+ if ( server . IsReplica )
90+ {
91+ continue ; // Skip replica nodes
92+ }
93+ // Send your command to the master node
9994
100- public static async Task < List < RedisResult > > ExecuteBroadcastAsync ( this IDatabaseAsync db , string command )
101- => await db . ExecuteBroadcastAsync ( new SerializedCommand ( command ) ) ;
95+ results . Add ( server . Multiplexer . GetDatabase ( ) . Execute ( command ) ) ;
96+ }
97+ return results ;
98+ }
10299
103- private static async Task < List < RedisResult > > ExecuteBroadcastAsync ( this IDatabaseAsync db , SerializedCommand command )
104- {
105- var redis = db . Multiplexer ;
106- var endpoints = redis . GetEndPoints ( ) ;
107- var results = new List < RedisResult > ( endpoints . Length ) ;
100+ public static async Task < List < RedisResult > > ExecuteBroadcastAsync ( this IDatabaseAsync db , string command )
101+ => await db . ExecuteBroadcastAsync ( new SerializedCommand ( command ) ) ;
108102
109- foreach ( var endPoint in endpoints )
103+ private static async Task < List < RedisResult > > ExecuteBroadcastAsync ( this IDatabaseAsync db , SerializedCommand command )
110104 {
111- var server = redis . GetServer ( endPoint ) ;
105+ var redis = db . Multiplexer ;
106+ var endpoints = redis . GetEndPoints ( ) ;
107+ var results = new List < RedisResult > ( endpoints . Length ) ;
112108
113- if ( server . IsReplica )
109+ foreach ( var endPoint in endpoints )
114110 {
115- continue ; // Skip replica nodes
116- }
117- // Send your command to the master node
111+ var server = redis . GetServer ( endPoint ) ;
112+
113+ if ( server . IsReplica )
114+ {
115+ continue ; // Skip replica nodes
116+ }
117+ // Send your command to the master node
118118
119- results . Add ( await server . Multiplexer . GetDatabase ( ) . ExecuteAsync ( command ) ) ;
119+ results . Add ( await server . Multiplexer . GetDatabase ( ) . ExecuteAsync ( command ) ) ;
120+ }
121+ return results ;
120122 }
121- return results ;
122- }
123123
124- public static string GetNRedisStackVersion ( )
125- {
126- Version version = typeof ( Auxiliary ) . Assembly . GetName ( ) . Version ! ;
127- return $ "{ version . Major } .{ version . Minor } .{ version . Build } ";
124+ public static string GetNRedisStackVersion ( )
125+ {
126+ Version version = typeof ( Auxiliary ) . Assembly . GetName ( ) . Version ! ;
127+ return $ "{ version . Major } .{ version . Minor } .{ version . Build } ";
128+ }
128129 }
129130}
0 commit comments