1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Linq ;
3
4
using System . Collections . Generic ;
4
5
using ServiceStack . Configuration ;
@@ -24,15 +25,23 @@ public class TemplateRedisFilters : TemplateFilter
24
25
{
25
26
public IRedisClientsManager RedisManager { get ; set ; }
26
27
public IAppSettings AppSettings { get ; set ; }
27
- T exec < T > ( Func < IRedisClient , T > fn )
28
+
29
+ T exec < T > ( Func < IRedisClient , T > fn , TemplateScopeContext scope , object options )
28
30
{
29
- using ( var db = RedisManager . GetClient ( ) )
31
+ try
32
+ {
33
+ using ( var db = RedisManager . GetClient ( ) )
34
+ {
35
+ return fn ( db ) ;
36
+ }
37
+ }
38
+ catch ( Exception ex )
30
39
{
31
- return fn ( db ) ;
40
+ throw new StopFilterExecutionException ( scope , options , ex ) ;
32
41
}
33
42
}
34
43
35
- static Dictionary < string , int > cmdArgCounts = new Dictionary < string , int > ( StringComparer . OrdinalIgnoreCase ) {
44
+ static readonly Dictionary < string , int > cmdArgCounts = new Dictionary < string , int > ( StringComparer . OrdinalIgnoreCase ) {
36
45
{ "SET" , 3 }
37
46
} ;
38
47
@@ -81,14 +90,34 @@ object toObject(RedisText r)
81
90
return r . Text ;
82
91
}
83
92
84
- public object redisCall ( string cmd )
93
+ public object redisCall ( TemplateScopeContext scope , object redisCommand ) => redisCall ( scope , redisCommand , null ) ;
94
+ public object redisCall ( TemplateScopeContext scope , object redisCommand , object options )
85
95
{
86
- if ( string . IsNullOrEmpty ( cmd ) )
96
+ if ( redisCommand == null )
87
97
return null ;
88
98
89
- var args = parseCommandString ( cmd ) ;
99
+ List < string > args ;
100
+ if ( redisCommand is string cmd )
101
+ {
102
+ if ( string . IsNullOrEmpty ( cmd ) )
103
+ return null ;
104
+
105
+ args = parseCommandString ( cmd ) ;
106
+ }
107
+ else if ( redisCommand is IEnumerable e && ! ( e is IDictionary ) )
108
+ {
109
+ args = new List < string > ( ) ;
110
+ foreach ( var arg in e )
111
+ {
112
+ if ( arg == null ) continue ;
113
+ args . Add ( arg . ToString ( ) ) ;
114
+ }
115
+ }
116
+ else
117
+ throw new NotSupportedException ( $ "redisCall expects a string or an object args but received a { redisCommand . GetType ( ) . Name } instead.") ;
118
+
90
119
var objParams = args . Select ( x => ( object ) x ) . ToArray ( ) ;
91
- var redisText = exec ( r => r . Custom ( objParams ) ) ;
120
+ var redisText = exec ( r => r . Custom ( objParams ) , scope , options ) ;
92
121
var result = toObject ( redisText ) ;
93
122
return result ;
94
123
}
@@ -110,12 +139,14 @@ public string redisSearchKeysAsJson(TemplateScopeContext scope, string query, ob
110
139
if ( string . IsNullOrEmpty ( query ) )
111
140
return null ;
112
141
113
- var args = scope . AssertOptions ( nameof ( redisSearchKeys ) , options ) ;
114
- var limit = args . TryGetValue ( "limit" , out object value )
115
- ? value . ConvertTo < int > ( )
116
- : scope . GetValue ( "redis.search.limit" ) ?? 100 ;
142
+ try
143
+ {
144
+ var args = scope . AssertOptions ( nameof ( redisSearchKeys ) , options ) ;
145
+ var limit = args . TryGetValue ( "limit" , out object value )
146
+ ? value . ConvertTo < int > ( )
147
+ : scope . GetValue ( "redis.search.limit" ) ?? 100 ;
117
148
118
- const string LuaScript = @"
149
+ const string LuaScript = @"
119
150
local limit = tonumber(ARGV[2])
120
151
local pattern = ARGV[1]
121
152
local cursor = tonumber(ARGV[3])
@@ -157,10 +188,15 @@ public string redisSearchKeysAsJson(TemplateScopeContext scope, string query, ob
157
188
cursorAttrs['results'] = keyAttrs
158
189
return cjson.encode(cursorAttrs)" ;
159
190
160
- var json = exec ( r => r . ExecCachedLua ( LuaScript , sha1 =>
161
- r . ExecLuaShaAsString ( sha1 , query , limit . ToString ( ) , "0" ) ) ) ;
191
+ var json = exec ( r => r . ExecCachedLua ( LuaScript , sha1 =>
192
+ r . ExecLuaShaAsString ( sha1 , query , limit . ToString ( ) , "0" ) ) , scope , options ) ;
162
193
163
- return json ;
194
+ return json ;
195
+ }
196
+ catch ( Exception ex )
197
+ {
198
+ throw new StopFilterExecutionException ( scope , options , ex ) ;
199
+ }
164
200
}
165
201
}
166
202
}
0 commit comments