|
| 1 | +using System.Text.Json; |
| 2 | +using StackExchange.Redis; |
| 3 | + |
| 4 | +namespace Juggle.Domain.Engine.NodeExecutors; |
| 5 | + |
| 6 | +/// <summary>Redis 连接管理(按连接串缓存复用)。</summary> |
| 7 | +public static class RedisConnectionPool |
| 8 | +{ |
| 9 | + private static readonly Dictionary<string, ConnectionMultiplexer> _cache = new(); |
| 10 | + |
| 11 | + public static ConnectionMultiplexer Get(string connStr) |
| 12 | + { |
| 13 | + lock (_cache) |
| 14 | + { |
| 15 | + if (!_cache.TryGetValue(connStr, out var conn) || !conn.IsConnected) |
| 16 | + { |
| 17 | + conn?.Dispose(); |
| 18 | + conn = ConnectionMultiplexer.Connect(connStr); |
| 19 | + _cache[connStr] = conn; |
| 20 | + } |
| 21 | + return conn; |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// <summary>Redis 缓存查询节点:按 key 取值(JSON 字符串自动解析为对象)。</summary> |
| 27 | +public class RedisGetNodeExecutor : INodeExecutor |
| 28 | +{ |
| 29 | + private readonly string _connStr; |
| 30 | + |
| 31 | + public RedisGetNodeExecutor(string connStr) => _connStr = connStr; |
| 32 | + |
| 33 | + public async Task<string?> ExecuteAsync(FlowNode node, FlowContext context) |
| 34 | + { |
| 35 | + var cfg = node.RedisGetConfig |
| 36 | + ?? throw new InvalidOperationException($"Redis 查询节点 [{node.Key}] 未配置 redisGetConfig。"); |
| 37 | + if (string.IsNullOrWhiteSpace(cfg.Key)) |
| 38 | + throw new InvalidOperationException($"Redis 查询节点 [{node.Key}] 未配置 key。"); |
| 39 | + |
| 40 | + var key = RenderTemplate(cfg.Key, context); |
| 41 | + var db = RedisConnectionPool.Get(_connStr).GetDatabase(); |
| 42 | + |
| 43 | + var value = await db.StringGetAsync(key); |
| 44 | + object? result = null; |
| 45 | + if (value.HasValue) |
| 46 | + { |
| 47 | + var str = value.ToString(); |
| 48 | + // JSON 字符串自动解析为对象/数组 |
| 49 | + try |
| 50 | + { |
| 51 | + using var doc = JsonDocument.Parse(str); |
| 52 | + result = FileParseNodeExecutor.CloneToNative(doc.RootElement); |
| 53 | + } |
| 54 | + catch { result = str; } |
| 55 | + } |
| 56 | + |
| 57 | + if (!string.IsNullOrWhiteSpace(cfg.Output)) |
| 58 | + context.SetVariable(cfg.Output, result); |
| 59 | + |
| 60 | + return node.Outgoings.FirstOrDefault(); |
| 61 | + } |
| 62 | + |
| 63 | + internal static string RenderTemplate(string template, FlowContext context) |
| 64 | + => System.Text.RegularExpressions.Regex.Replace(template, @"\$\{([^}]+)\}", m => |
| 65 | + { |
| 66 | + var name = m.Groups[1].Value.Trim(); |
| 67 | + return context.GetVariable(name)?.ToString() ?? ""; |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +/// <summary>Redis 缓存设置节点:写入 key-value(可设过期秒数),结果写入输出变量。</summary> |
| 72 | +public class RedisSetNodeExecutor : INodeExecutor |
| 73 | +{ |
| 74 | + private readonly string _connStr; |
| 75 | + |
| 76 | + public RedisSetNodeExecutor(string connStr) => _connStr = connStr; |
| 77 | + |
| 78 | + public async Task<string?> ExecuteAsync(FlowNode node, FlowContext context) |
| 79 | + { |
| 80 | + var cfg = node.RedisSetConfig |
| 81 | + ?? throw new InvalidOperationException($"Redis 设置节点 [{node.Key}] 未配置 redisSetConfig。"); |
| 82 | + if (string.IsNullOrWhiteSpace(cfg.Key)) |
| 83 | + throw new InvalidOperationException($"Redis 设置节点 [{node.Key}] 未配置 key。"); |
| 84 | + |
| 85 | + var key = RedisGetNodeExecutor.RenderTemplate(cfg.Key, context); |
| 86 | + var val = string.IsNullOrWhiteSpace(cfg.Value) |
| 87 | + ? "" |
| 88 | + : context.GetVariable(cfg.Value) switch |
| 89 | + { |
| 90 | + string s => s, |
| 91 | + var v => JsonSerializer.Serialize(v) |
| 92 | + }; |
| 93 | + var db = RedisConnectionPool.Get(_connStr).GetDatabase(); |
| 94 | + |
| 95 | + var ok = cfg.ExpireSeconds > 0 |
| 96 | + ? await db.StringSetAsync(key, val, TimeSpan.FromSeconds(cfg.ExpireSeconds)) |
| 97 | + : await db.StringSetAsync(key, val); |
| 98 | + |
| 99 | + if (!string.IsNullOrWhiteSpace(cfg.Output)) |
| 100 | + context.SetVariable(cfg.Output, ok); |
| 101 | + |
| 102 | + return node.Outgoings.FirstOrDefault(); |
| 103 | + } |
| 104 | +} |
0 commit comments