/
RedisScripts.cs
284 lines (245 loc) · 9.54 KB
/
RedisScripts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using ServiceStack.Script;
namespace ServiceStack.Redis;
public class RedisSearchCursorResult
{
public int Cursor { get; set; }
public List<RedisSearchResult> Results { get; set; }
}
public class RedisSearchResult
{
public string Id { get; set; }
public string Type { get; set; }
public long Ttl { get; set; }
public long Size { get; set; }
}
[Obsolete("Use RedisScripts")]
public class TemplateRedisFilters : RedisScripts {}
public class RedisScripts : ScriptMethods
{
private const string RedisConnection = "__redisConnection";
private IRedisClientsManager redisManager;
public IRedisClientsManager RedisManager
{
get => redisManager ??= Context.Container.Resolve<IRedisClientsManager>();
set => redisManager = value;
}
T exec<T>(Func<IRedisClient, T> fn, ScriptScopeContext scope, object options)
{
try
{
if ((options is Dictionary<string, object> obj && obj.TryGetValue("connectionString", out var oRedisConn))
|| scope.PageResult.Args.TryGetValue(RedisConnection, out oRedisConn))
{
using var redis = new RedisClient((string)oRedisConn);
return fn(redis);
}
using (var redis = RedisManager.GetClient())
{
return fn(redis);
}
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options, ex);
}
}
public IgnoreResult useRedis(ScriptScopeContext scope, string redisConnection)
{
if (redisConnection == null)
scope.PageResult.Args.Remove(RedisConnection);
else
scope.PageResult.Args[RedisConnection] = redisConnection;
return IgnoreResult.Value;
}
static readonly Dictionary<string, int> cmdArgCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) {
{ "SET", 3 }
};
List<string> parseCommandString(string cmd)
{
var args = new List<string>();
var lastPos = 0;
for (var i = 0; i < cmd.Length; i++)
{
var c = cmd[i];
if (c is '{' or '[')
{
break; //stop splitting args if value is complex type
}
if (c == ' ')
{
var arg = cmd.Substring(lastPos, i - lastPos);
args.Add(arg);
lastPos = i + 1;
//if we've reached the command args count, capture the rest of the body as the last arg
if (cmdArgCounts.TryGetValue(args[0], out int argCount) && args.Count == argCount - 1)
break;
}
}
args.Add(cmd.Substring(lastPos));
return args;
}
object toObject(RedisText r)
{
if (r == null)
return null;
if (r.Children != null && r.Children.Count > 0)
{
var to = new List<object>();
for (var i = 0; i < r.Children.Count; i++)
{
var child = r.Children[i];
var value = child.Text ?? toObject(child);
to.Add(value);
}
return to;
}
return r.Text;
}
public object redisCall(ScriptScopeContext scope, object redisCommand) => redisCall(scope, redisCommand, null);
public object redisCall(ScriptScopeContext scope, object redisCommand, object options)
{
if (redisCommand == null)
return null;
List<string> args;
if (redisCommand is string cmd)
{
if (string.IsNullOrEmpty(cmd))
return null;
args = parseCommandString(cmd);
}
else if (redisCommand is IEnumerable e && !(e is IDictionary))
{
args = new List<string>();
foreach (var arg in e)
{
if (arg == null) continue;
args.Add(arg.ToString());
}
}
else
throw new NotSupportedException($"redisCall expects a string or an object args but received a {redisCommand.GetType().Name} instead.");
var objParams = args.Select(x => (object)x).ToArray();
var redisText = exec(r => r.Custom(objParams), scope, options);
var result = toObject(redisText);
return result;
}
public List<RedisSearchResult> redisSearchKeys(ScriptScopeContext scope, string query) => redisSearchKeys(scope, query, null);
public List<RedisSearchResult> redisSearchKeys(ScriptScopeContext scope, string query, object options)
{
var json = redisSearchKeysAsJson(scope, query, options);
const string noResult = "{\"cursor\":0,\"results\":{}}";
if (json == noResult)
return new List<RedisSearchResult>();
var searchResults = json.FromJson<RedisSearchCursorResult>();
return searchResults.Results;
}
public Dictionary<string, string> redisInfo(ScriptScopeContext scope) => redisInfo(scope, null);
public Dictionary<string, string> redisInfo(ScriptScopeContext scope, object options) => exec(r => r.Info, scope, options);
public string redisConnectionString(ScriptScopeContext scope) => exec(r => $"{r.Host}:{r.Port}?db={r.Db}", scope, null);
public Dictionary<string, object> redisConnection(ScriptScopeContext scope) => exec(r => new Dictionary<string, object>
{
{ "host", r.Host },
{ "port", r.Port },
{ "db", r.Db },
}, scope, null);
public string redisToConnectionString(ScriptScopeContext scope, object connectionInfo) => redisToConnectionString(scope, connectionInfo, null);
public string redisToConnectionString(ScriptScopeContext scope, object connectionInfo, object options)
{
var connectionString = connectionInfo as string;
if (connectionString != null)
return connectionString;
if (connectionInfo is IDictionary<string, object> d)
{
var host = (d.TryGetValue("host", out object h) ? h as string : null) ?? "localhost";
var port = d.TryGetValue("port", out object p) ? DynamicInt.Instance.ConvertFrom(p) : 6379;
var db = d.TryGetValue("db", out object oDb) ? DynamicInt.Instance.ConvertFrom(oDb) : 0;
connectionString = $"{host}:{port}?db={db}";
if (d.TryGetValue("password", out object password))
connectionString += "&password=" + password.ToString().UrlEncode();
}
return connectionString;
}
public string redisChangeConnection(ScriptScopeContext scope, object newConnection) => redisChangeConnection(scope, newConnection, null);
public string redisChangeConnection(ScriptScopeContext scope, object newConnection, object options)
{
try
{
var connectionString = redisToConnectionString(scope, newConnection, options);
if (connectionString == null)
throw new NotSupportedException(nameof(redisChangeConnection) + " expects a String or an ObjectDictionary but received: " + (newConnection?.GetType().Name ?? "null"));
using (var testConnection = new RedisClient(connectionString))
{
testConnection.Ping();
}
((IRedisFailover)RedisManager).FailoverTo(connectionString);
return connectionString;
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options ?? newConnection as IDictionary<string,object>, ex);
}
}
public string redisSearchKeysAsJson(ScriptScopeContext scope, string query, object options)
{
if (string.IsNullOrEmpty(query))
return null;
try
{
var args = scope.AssertOptions(nameof(redisSearchKeys), options);
var limit = args.TryGetValue("limit", out object value)
? value.ConvertTo<int>()
: scope.GetValue("redis.search.limit") ?? 100;
const string LuaScript = @"
local limit = tonumber(ARGV[2])
local pattern = ARGV[1]
local cursor = tonumber(ARGV[3])
local len = 0
local keys = {}
repeat
local r = redis.call('scan', cursor, 'MATCH', pattern, 'COUNT', limit)
cursor = tonumber(r[1])
for k,v in ipairs(r[2]) do
table.insert(keys, v)
len = len + 1
if len == limit then break end
end
until cursor == 0 or len == limit
local cursorAttrs = {['cursor'] = cursor, ['results'] = {}}
if len == 0 then
return cjson.encode(cursorAttrs)
end
local keyAttrs = {}
for i,key in ipairs(keys) do
local type = redis.call('type', key)['ok']
local pttl = redis.call('pttl', key)
local size = 0
if type == 'string' then
size = redis.call('strlen', key)
elseif type == 'list' then
size = redis.call('llen', key)
elseif type == 'set' then
size = redis.call('scard', key)
elseif type == 'zset' then
size = redis.call('zcard', key)
elseif type == 'hash' then
size = redis.call('hlen', key)
end
local attrs = {['id'] = key, ['type'] = type, ['ttl'] = pttl, ['size'] = size, ['foo'] = 'bar'}
table.insert(keyAttrs, attrs)
end
cursorAttrs['results'] = keyAttrs
return cjson.encode(cursorAttrs)";
var json = exec(r => r.ExecCachedLua(LuaScript, sha1 =>
r.ExecLuaShaAsString(sha1, query, limit.ToString(), "0")), scope, options);
return json;
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options, ex);
}
}
}