Repository navigation
Expand file tree
/
Copy pathJsonCommandsAsync.cs
More file actions
241 lines (198 loc) · 8.96 KB
/
Copy pathJsonCommandsAsync.cs
File metadata and controls
241 lines (198 loc) · 8.96 KB
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
using System.ComponentModel;
using NRedisStack.Json.DataTypes;
using StackExchange.Redis;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace NRedisStack;
public class JsonCommandsAsync(IDatabaseAsync db) : IJsonCommandsAsync
{
public async Task<long?[]> ArrAppendAsync(RedisKey key, string? path = null, params object[] values)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ArrAppend(key, path, values))).ToNullableLongArray();
}
public async Task<long?[]> ArrIndexAsync(RedisKey key, string path, object value, long? start = null,
long? stop = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ArrIndex(key, path, value, start, stop)))
.ToNullableLongArray();
}
public async Task<long?[]> ArrInsertAsync(RedisKey key, string path, long index, params object[] values)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ArrInsert(key, path, index, values))).ToNullableLongArray();
}
public async Task<long?[]> ArrLenAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ArrLen(key, path))).ToNullableLongArray();
}
public async Task<RedisResult[]> ArrPopAsync(RedisKey key, string? path = null, long? index = null)
{
RedisResult result = await db.ExecuteAsync(JsonCommandBuilder.ArrPop(key, path, index));
return result.Resp2Type switch
{
ResultType.Array => (RedisResult[])result!,
ResultType.BulkString => [result],
_ => []
};
}
public async Task<long?[]> ArrTrimAsync(RedisKey key, string path, long start, long stop) =>
(await db.ExecuteAsync(JsonCommandBuilder.ArrTrim(key, path, start, stop))).ToNullableLongArray();
public async Task<long> ClearAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.Clear(key, path))).ToLong();
}
public async Task<long> DelAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.Del(key, path))).ToLong();
}
public Task<long> ForgetAsync(RedisKey key, string? path = null) => DelAsync(key, path);
public async Task<RedisResult> GetAsync(RedisKey key, RedisValue? indent = null, RedisValue? newLine = null,
RedisValue? space = null,
RedisValue? path = null)
{
return await db.ExecuteAsync(JsonCommandBuilder.Get(key, indent, newLine, space, path));
}
public async Task<RedisResult> GetAsync(RedisKey key, string[] paths, RedisValue? indent = null,
RedisValue? newLine = null,
RedisValue? space = null)
{
return await db.ExecuteAsync(JsonCommandBuilder.Get(key, paths, indent, newLine, space));
}
public async Task<T?> GetAsync<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = null)
{
var res = await db.ExecuteAsync(JsonCommandBuilder.Get<T>(key, path));
if (res.Resp2Type == ResultType.BulkString && !res.IsNull)
{
var arr = JsonSerializer.Deserialize<JsonArray>(res.ToString());
if (arr?.Count > 0)
{
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(arr[0]), serializerOptions);
}
}
return default;
}
/// <inheritdoc/>
public async Task<IEnumerable<T?>> GetEnumerableAsync<T>(RedisKey key, string path = "$")
{
RedisResult res = await db.ExecuteAsync(JsonCommandBuilder.Get<T>(key, path));
return JsonSerializer.Deserialize<IEnumerable<T>>(res.ToString())!;
}
public async Task<RedisResult[]> MGetAsync(RedisKey[] keys, string path)
{
return (await db.ExecuteAsync(JsonCommandBuilder.MGet(keys, path))).ToArray();
}
public async Task<double?[]> NumIncrbyAsync(RedisKey key, string path, double value)
{
var res = await db.ExecuteAsync(JsonCommandBuilder.NumIncrby(key, path, value));
return ResponseParser.ParseJsonDoubleArray(res);
}
public async Task<IEnumerable<HashSet<string>>> ObjKeysAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ObjKeys(key, path))).ToHashSets();
}
public async Task<long?[]> ObjLenAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.ObjLen(key, path))).ToNullableLongArray();
}
public async Task<RedisResult[]> RespAsync(RedisKey key, string? path = null)
{
RedisResult result = await db.ExecuteAsync(JsonCommandBuilder.Resp(key, path));
if (result.IsNull)
{
return [];
}
return (RedisResult[])result!;
}
/// <inheritdoc/>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when, JsonSerializerOptions? serializerOptions)
=> SetAsync(key, path, obj, when, serializerOptions, JsonNumericArrayStorage.NotSpecified);
/// <inheritdoc/>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json, When when)
=> SetAsync(key, path, json, when, JsonNumericArrayStorage.NotSpecified);
/// <inheritdoc/>
public Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always,
JsonSerializerOptions? serializerOptions = default, JsonNumericArrayStorage fpha = JsonNumericArrayStorage.NotSpecified)
{
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
return SetAsync(key, path, json, when, fpha);
}
public async Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always, JsonNumericArrayStorage fpha = JsonNumericArrayStorage.NotSpecified)
{
return (await db.ExecuteAsync(JsonCommandBuilder.Set(key, path, json, when, fpha))).OKtoBoolean();
}
public async Task<bool> MSetAsync(KeyPathValue[] KeyPathValueList)
{
return (await db.ExecuteAsync(JsonCommandBuilder.MSet(KeyPathValueList))).OKtoBoolean();
}
/// <inheritdoc/>
public async Task<bool> MergeAsync(RedisKey key, RedisValue path, RedisValue json)
{
return (await db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean();
}
/// <inheritdoc/>
public async Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj,
JsonSerializerOptions? serializerOptions = default)
{
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
return (await db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean();
}
public async Task<bool> SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"File {filePath} not found.");
}
string fileContent = File.ReadAllText(filePath);
return await SetAsync(key, path, fileContent, when);
}
public async Task<int> SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always)
{
int inserted = 0;
string key;
var files = Directory.EnumerateFiles(filesPath, "*.json");
foreach (var filePath in files)
{
key = filePath.Substring(0, filePath.IndexOf(".", StringComparison.Ordinal));
if (await SetFromFileAsync(key, path, filePath, when))
{
inserted++;
}
}
foreach (var dirPath in Directory.EnumerateDirectories(filesPath))
{
inserted += await SetFromDirectoryAsync(path, dirPath, when);
}
return inserted;
}
public async Task<long?[]> StrAppendAsync(RedisKey key, string value, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.StrAppend(key, value, path))).ToNullableLongArray();
}
public async Task<long?[]> StrLenAsync(RedisKey key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.StrLen(key, path))).ToNullableLongArray();
}
public async Task<bool?[]> ToggleAsync(RedisKey key, string? path = null)
{
RedisResult result = await db.ExecuteAsync(JsonCommandBuilder.Toggle(key, path));
if (result.IsNull)
{
return [];
}
if (result.Resp2Type == ResultType.Integer)
{
return [(long)result == 1];
}
return ((RedisResult[])result!).Select(x => (bool?)((long)x == 1)).ToArray();
}
public async Task<JsonType[]> TypeAsync(RedisKey key, string? path = null)
{
RedisResult result = await db.ExecuteAsync(JsonCommandBuilder.Type(key, path));
return ResponseParser.ParseJsonTypeArray(result);
}
public async Task<long> DebugMemoryAsync(string key, string? path = null)
{
return (await db.ExecuteAsync(JsonCommandBuilder.DebugMemory(key, path))).ToLong();
}
}