forked from MiniProfiler/dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiStorageProvider.cs
More file actions
324 lines (300 loc) · 13.6 KB
/
MultiStorageProvider.cs
File metadata and controls
324 lines (300 loc) · 13.6 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StackExchange.Profiling.Storage
{
/// <summary>
/// Allow for results to be stored in and retrieved from multiple IAsyncStorage stores.
/// When reading Loading a MiniProfiler, will load from the first Store that returns a record for the Guid.
/// When saving, will save in all Stores.
/// </summary>
/// <example>Ideal usage scenario - you want to store requests in Cache and Sql Server, but only want to retrieve from Cache if it is available</example>
public class MultiStorageProvider : IAdvancedAsyncStorage
{
/// <summary>
/// The stores that are exposed by this <see cref="MultiStorageProvider"/>
/// </summary>
public List<IAsyncStorage> Stores { get; set; }
/// <summary>
/// Should operations use Parallel.ForEach when it makes sense to do so (all save operations, and data retrieval where all items in <see cref="Stores"/> are hit?
/// If False, all operations will run synchronously, in order. Defaults to False.
/// </summary>
public bool AllowParallelOps { get; set; }
/// <summary>
/// Create the <see cref="MultiStorageProvider"/> with the given collection of <see cref="IAsyncStorage"/> objects (order is important!)
/// </summary>
/// <param name="stores">The <see cref="IAsyncStorage"/> objects to use for storage (order is important!)</param>
/// <exception cref="ArgumentNullException">Throws when there are no Stores.</exception>
public MultiStorageProvider(params IAsyncStorage[] stores)
{
Stores = stores.Where(x => x != null).ToList();
if (Stores.Count == 0)
{
throw new ArgumentNullException(nameof(stores), "Please include at least one IAsyncStorage object when initializing a MultiStorageProvider");
}
}
/// <summary>
/// Run the List command on the first Store from <see cref="Stores"/> that returns a result with any values.
/// Will NOT return a superset of results from all <see cref="Stores"/>.
/// </summary>
/// <param name="maxResults">The maximum number of results to return.</param>
/// <param name="start">(Optional) The start of the date range to fetch.</param>
/// <param name="finish">(Optional) The end of the date range to fetch.</param>
/// <param name="orderBy">(Optional) The order to fetch profiler IDs in.</param>
public IEnumerable<Guid> List(int maxResults, DateTime? start = null, DateTime? finish = null, ListResultsOrder orderBy = ListResultsOrder.Descending)
{
if (Stores != null)
{
foreach (var store in Stores)
{
var results = store.List(maxResults, start, finish, orderBy);
if (results?.Any() == true)
{
return results;
}
}
}
return Enumerable.Empty<Guid>();
}
/// <summary>
/// Asynchronously run the List command on the first Store from <see cref="Stores"/> that returns a result with any values.
/// Will NOT return a superset of results from all <see cref="Stores"/>.
/// </summary>
/// <param name="maxResults">The maximum number of results to return.</param>
/// <param name="start">(Optional) The start of the date range to fetch.</param>
/// <param name="finish">(Optional) The end of the date range to fetch.</param>
/// <param name="orderBy">(Optional) The order to fetch profiler IDs in.</param>
public async Task<IEnumerable<Guid>> ListAsync(int maxResults, DateTime? start = null, DateTime? finish = null, ListResultsOrder orderBy = ListResultsOrder.Descending)
{
if (Stores == null) return Enumerable.Empty<Guid>();
foreach (var store in Stores)
{
var results = await store.ListAsync(maxResults, start, finish, orderBy).ConfigureAwait(false);
if (results?.Any() == true)
{
return results;
}
}
return Enumerable.Empty<Guid>();
}
/// <summary>
/// Stores <paramref name="profiler"/> under its <see cref="MiniProfiler.Id"/> in all of the <see cref="Stores"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
/// <remarks>
/// Should also ensure the profiler is stored as being unviewed by its profiling <see cref="MiniProfiler.User"/>.
/// </remarks>
public void Save(MiniProfiler profiler)
{
if (Stores == null) return;
if (AllowParallelOps)
{
Parallel.ForEach(Stores, x => x.Save(profiler));
}
else
{
foreach (var s in Stores)
{
s.Save(profiler);
}
}
}
/// <summary>
/// Asynchronously stores <paramref name="profiler"/> under its <see cref="MiniProfiler.Id"/> in all of the <see cref="Stores"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
/// <remarks>
/// Should also ensure the profiler is stored as being unviewed by its profiling <see cref="MiniProfiler.User"/>.
/// </remarks>
public Task SaveAsync(MiniProfiler profiler)
{
if (Stores == null) return Task.CompletedTask;
return Task.WhenAll(Stores.Select(s => s.SaveAsync(profiler)));
}
/// <summary>
/// Returns a <see cref="MiniProfiler"/> from storage based on <paramref name="id"/>,
/// which should map to <see cref="MiniProfiler.Id"/>. Will check in all of the <see cref="IAsyncStorage"/>
/// classes in <see cref="Stores"/>, and will return the first <see cref="MiniProfiler"/> that it finds.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
/// <remarks>
/// Should also update that the resulting profiler has been marked as viewed by its profiling <see cref="MiniProfiler.User"/>.
/// </remarks>
public MiniProfiler Load(Guid id)
{
if (Stores == null) return null;
foreach (var store in Stores)
{
var result = store.Load(id);
if (result != null)
{
return result;
}
}
return null;
}
/// <summary>
/// Asynchronously returns a <see cref="MiniProfiler"/> from storage based on <paramref name="id"/>,
/// which should map to <see cref="MiniProfiler.Id"/>. Will check in all of the <see cref="IAsyncStorage"/>
/// classes in <see cref="Stores"/>, and will return the first <see cref="MiniProfiler"/> that it finds.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
/// <remarks>
/// Should also update that the resulting profiler has been marked as viewed by its profiling <see cref="MiniProfiler.User"/>.
/// </remarks>
public async Task<MiniProfiler> LoadAsync(Guid id)
{
if (Stores == null) return null;
foreach (var store in Stores)
{
var result = await store.LoadAsync(id).ConfigureAwait(false);
if (result != null)
{
return result;
}
}
return null;
}
/// <summary>
/// Whether any of the underlying providers should call SetUnviewed methods after saving.
/// </summary>
public bool SetUnviewedAfterSave { get; }
/// <summary>
/// Sets a particular profiler session so it is considered "unviewed".
/// Will set this to all <see cref="IAsyncStorage"/> items in <see cref="Stores"/>
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
public void SetUnviewed(string user, Guid id)
{
if (Stores == null) return;
if (AllowParallelOps)
{
Parallel.ForEach(Stores, x => x.SetUnviewed(user, id));
}
else
{
foreach (var s in Stores)
{
s.SetUnviewed(user, id);
}
}
}
/// <summary>
/// Asynchronously sets a particular profiler session so it is considered "unviewed".
/// Will set this to all <see cref="IAsyncStorage"/> items in <see cref="Stores"/>
/// </summary>
/// <param name="user">The user to set this profiler ID as unviewed for.</param>
/// <param name="id">The profiler ID to set unviewed.</param>
public Task SetUnviewedAsync(string user, Guid id)
{
if (Stores == null) return Task.CompletedTask;
return Task.WhenAll(Stores.Select(s => s.SetUnviewedAsync(user, id)));
}
/// <summary>
/// Sets a particular profiler session to "viewed".
/// Will set this to all <see cref="IAsyncStorage"/> items in <see cref="Stores"/>
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="id">The profiler ID to set viewed.</param>
public void SetViewed(string user, Guid id)
{
if (Stores == null) return;
if (AllowParallelOps)
{
Parallel.ForEach(Stores, x => x.SetViewed(user, id));
}
else
{
foreach (var s in Stores)
{
s.SetViewed(user, id);
}
}
}
/// <summary>
/// Asynchronously sets a particular profiler session to "viewed".
/// This sets viewed on all <see cref="IAsyncStorage"/> items in <see cref="Stores"/>.
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="id">The profiler ID to set viewed.</param>
public Task SetViewedAsync(string user, Guid id)
{
if (Stores == null) return Task.CompletedTask;
return Task.WhenAll(Stores.Select(s => s.SetViewedAsync(user, id)));
}
/// <summary>
/// Asynchronously sets the provided profiler sessions to "viewed"
/// </summary>
/// <param name="user">The user to set this profiler ID as viewed for.</param>
/// <param name="ids">The profiler IDs to set viewed.</param>
public Task SetViewedAsync(string user, IEnumerable<Guid> ids)
{
if (Stores == null) return Task.CompletedTask;
return Task.WhenAll(Stores.Select(async s =>
{
if (s is IAdvancedAsyncStorage storage)
{
await storage.SetViewedAsync(user, ids).ConfigureAwait(false);
}
else
{
foreach (var id in ids)
{
await s.SetViewedAsync(user, id).ConfigureAwait(false);
}
}
}));
}
/// <summary>
/// Runs <see cref="IAsyncStorage.GetUnviewedIds"/> on each <see cref="IAsyncStorage"/> object in <see cref="Stores"/> and returns the Union of results.
/// Will run on multiple stores in parallel if <see cref="AllowParallelOps"/> = true.
/// </summary>
/// <param name="user">The user to fetch IDs for</param>
/// <returns>A distinct list of unviewed IDs</returns>
public List<Guid> GetUnviewedIds(string user)
{
var results = new List<Guid>();
if (Stores == null) return results;
if (AllowParallelOps)
{
Parallel.ForEach(Stores, x =>
{
var result = x.GetUnviewedIds(user);
lock (results)
{
results.AddRange(result);
}
});
}
else
{
foreach (var s in Stores)
{
results.AddRange(s.GetUnviewedIds(user));
}
}
return results.Distinct().ToList(); // get rid of duplicates
}
/// <summary>
/// Asynchronously runs <see cref="IAsyncStorage.GetUnviewedIds"/> on each <see cref="IAsyncStorage"/> object in <see cref="Stores"/> and returns the Union of results.
/// </summary>
/// <param name="user">The user to fetch IDs for</param>
/// <returns>A distinct list of unviewed IDs</returns>
public async Task<List<Guid>> GetUnviewedIdsAsync(string user)
{
var results = new List<Guid>();
if (Stores == null) return results;
var tasks = Stores.Select(s => s.GetUnviewedIdsAsync(user));
await Task.WhenAll(tasks).ConfigureAwait(false);
foreach (var t in tasks)
{
results.AddRange(t.Result);
}
return results.Distinct().ToList(); // get rid of duplicates
}
}
}