@@ -23,16 +23,20 @@ public class RedisSearchResult
2323
2424 public class TemplateRedisFilters : TemplateFilter
2525 {
26- public IRedisClientsManager RedisManager { get ; set ; }
27- public IAppSettings AppSettings { get ; set ; }
26+ private IRedisClientsManager redisManager ;
27+ public IRedisClientsManager RedisManager
28+ {
29+ get => redisManager ?? ( redisManager = Context . Container . Resolve < IRedisClientsManager > ( ) ) ;
30+ set => redisManager = value ;
31+ }
2832
2933 T exec < T > ( Func < IRedisClient , T > fn , TemplateScopeContext scope , object options )
3034 {
3135 try
3236 {
33- using ( var db = RedisManager . GetClient ( ) )
37+ using ( var redis = RedisManager . GetClient ( ) )
3438 {
35- return fn ( db ) ;
39+ return fn ( redis ) ;
3640 }
3741 }
3842 catch ( Exception ex )
@@ -134,6 +138,54 @@ public List<RedisSearchResult> redisSearchKeys(TemplateScopeContext scope, strin
134138 return searchResults . Results ;
135139 }
136140
141+ public string redisConnectionString ( TemplateScopeContext scope ) => exec ( r => $ "{ r . Host } :{ r . Port } ?db={ r . Db } ", scope , null ) ;
142+
143+ public string redisToConnectionString ( TemplateScopeContext scope , object connectionInfo ) => redisToConnectionString ( scope , connectionInfo , null ) ;
144+ public string redisToConnectionString ( TemplateScopeContext scope , object connectionInfo , object options )
145+ {
146+ var connectionString = connectionInfo as string ;
147+ if ( connectionString != null )
148+ return connectionString ;
149+
150+ if ( connectionInfo is IDictionary < string , object > d )
151+ {
152+ var host = ( d . TryGetValue ( "host" , out object h ) ? h as string : null ) ?? "localhost" ;
153+ var port = d . TryGetValue ( "port" , out object p ) ? DynamicInt . Instance . ConvertFrom ( p ) : 6379 ;
154+ var db = d . TryGetValue ( "db" , out object oDb ) ? DynamicInt . Instance . ConvertFrom ( oDb ) : 0 ;
155+
156+ connectionString = $ "{ host } :{ port } ?db={ db } ";
157+
158+ if ( d . TryGetValue ( "password" , out object password ) )
159+ connectionString += "&password=" + password . ToString ( ) . UrlEncode ( ) ;
160+ }
161+
162+ return connectionString ;
163+ }
164+
165+ public string redisChangeConnection ( TemplateScopeContext scope , object newConnection ) => redisChangeConnection ( scope , newConnection , null ) ;
166+ public string redisChangeConnection ( TemplateScopeContext scope , object newConnection , object options )
167+ {
168+ try
169+ {
170+ var connectionString = redisToConnectionString ( scope , newConnection , options ) ;
171+ if ( connectionString == null )
172+ throw new NotSupportedException ( nameof ( redisChangeConnection ) + " expects a String or an ObjectDictionary but received: " + ( newConnection ? . GetType ( ) . Name ?? "null" ) ) ;
173+
174+ using ( var testConnection = new RedisClient ( connectionString ) )
175+ {
176+ testConnection . Ping ( ) ;
177+ }
178+
179+ ( ( IRedisFailover ) RedisManager ) . FailoverTo ( connectionString ) ;
180+
181+ return connectionString ;
182+ }
183+ catch ( Exception ex )
184+ {
185+ throw new StopFilterExecutionException ( scope , options ?? newConnection as IDictionary < string , object > , ex ) ;
186+ }
187+ }
188+
137189 public string redisSearchKeysAsJson ( TemplateScopeContext scope , string query , object options )
138190 {
139191 if ( string . IsNullOrEmpty ( query ) )
0 commit comments