-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathMemoryCacheStorage.cs
More file actions
266 lines (240 loc) · 10.8 KB
/
MemoryCacheStorage.cs
File metadata and controls
266 lines (240 loc) · 10.8 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StackExchange.Profiling.Helpers;
using Microsoft.Extensions.Caching.Memory;
namespace StackExchange.Profiling.Storage
{
/// <summary>
/// A IMemoryCache-based provider for storing MiniProfiler instances (based on System.Runtime.Caching.MemoryCache)
/// </summary>
public class MemoryCacheStorage : IAsyncStorage
{
private readonly IMemoryCache _cache;
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0052:Remove unread private members", Justification = "API for later.")]
private MemoryCacheEntryOptions CacheEntryOptions { get; }
private readonly SortedList<ProfilerSortedKey, object> _profiles = new();
/// <summary>
/// The string that prefixes all keys that MiniProfilers are saved under, e.g.
/// <c>"mini-profiler-ecfb0050-7ce8-4bf1-bf82-2cb38e90e31e".</c>
/// </summary>
public const string CacheKeyPrefix = "mini-profiler-";
/// <summary>
/// Gets or sets how long to cache each <see cref="MiniProfiler"/> for, in absolute terms.
/// </summary>
public TimeSpan CacheDuration { get; set; }
/// <summary>
/// Creates a memory cache provider, storing each result in the provided IMemoryCache
/// for the specified duration.
/// </summary>
/// <param name="cache">The <see cref="IMemoryCache"/> to use for storage.</param>
/// <param name="cacheDuration">The duration to cache each profiler, before it expires from cache.</param>
/// <exception cref="ArgumentNullException">Throws when <paramref name="cache"/> is null.</exception>
public MemoryCacheStorage(IMemoryCache cache, TimeSpan cacheDuration)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
CacheDuration = cacheDuration;
CacheEntryOptions = new MemoryCacheEntryOptions { SlidingExpiration = cacheDuration };
}
private string GetCacheKey(Guid id) => CacheKeyPrefix + id.ToString();
/// <summary>
/// Returns a list of <see cref="MiniProfiler.Id"/>s that haven't been seen by <paramref name="user"/>.
/// </summary>
/// <param name="user">User identified by the current <c>MiniProfilerOptions.UserProvider</c></param>
public List<Guid> GetUnviewedIds(string user)
{
var ids = GetPerUserUnviewedIds(user);
lock (ids)
{
return new List<Guid>(ids);
}
}
/// <summary>
/// Returns a list of <see cref="MiniProfiler.Id"/>s that haven't been seen by <paramref name="user"/>.
/// </summary>
/// <param name="user">User identified by the current <c>MiniProfilerOptions.UserProvider</c></param>
public Task<List<Guid>> GetUnviewedIdsAsync(string user) => Task.FromResult(GetUnviewedIds(user));
private string GetPerUserUnviewedCacheKey(string user) => CacheKeyPrefix + "unviewed-for-user-" + user;
private List<Guid> GetPerUserUnviewedIds(string user)
{
var key = GetPerUserUnviewedCacheKey(user);
return _cache.Get(key) as List<Guid> ?? new List<Guid>();
}
/// <summary>
/// List the latest profiling results.
/// </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)
{
var guids = new List<Guid>();
lock (_profiles)
{
int idxStart = 0;
int idxFinish = _profiles.Count - 1;
if (start != null) idxStart = _profiles.BinaryClosestSearch(start.Value);
if (finish != null) idxFinish = _profiles.BinaryClosestSearch(finish.Value);
if (idxStart < 0) idxStart = 0;
if (idxFinish >= _profiles.Count) idxFinish = _profiles.Count - 1;
var keys = _profiles.Keys;
if (orderBy == ListResultsOrder.Ascending)
{
for (int i = idxStart; i <= idxFinish; i++)
{
guids.Add(keys[i].Id);
if (guids.Count == maxResults) break;
}
}
else
{
for (int i = idxFinish; i >= idxStart; i--)
{
guids.Add(keys[i].Id);
if (guids.Count == maxResults) break;
}
}
}
return guids;
}
/// <summary>
/// List the latest profiling results.
/// </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 Task<IEnumerable<Guid>> ListAsync(
int maxResults,
DateTime? start = null,
DateTime? finish = null,
ListResultsOrder orderBy = ListResultsOrder.Descending) => Task.FromResult(List(maxResults, start, finish, orderBy));
/// <summary>
/// Returns the saved <see cref="MiniProfiler"/> identified by <paramref name="id"/>. Also marks the resulting
/// profiler <see cref="MiniProfiler.HasUserViewed"/> to true.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
public MiniProfiler Load(Guid id) => _cache.Get(GetCacheKey(id)) as MiniProfiler;
/// <summary>
/// Returns the saved <see cref="MiniProfiler"/> identified by <paramref name="id"/>. Also marks the resulting
/// profiler <see cref="MiniProfiler.HasUserViewed"/> to true.
/// </summary>
/// <param name="id">The profiler ID to load.</param>
/// <returns>The loaded <see cref="MiniProfiler"/>.</returns>
public Task<MiniProfiler> LoadAsync(Guid id) => Task.FromResult(Load(id));
/// <summary>
/// Saves <paramref name="profiler"/> to the HttpRuntime.Cache under a key concatenated with <see cref="CacheKeyPrefix"/>
/// and the parameter's <see cref="MiniProfiler.Id"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
public void Save(MiniProfiler profiler)
{
if (profiler == null) return;
// use insert instead of add; add fails if the item already exists
_cache.Set(GetCacheKey(profiler.Id), profiler, DateTime.UtcNow + CacheDuration);
var profileInfo = new ProfilerSortedKey(profiler);
lock (_profiles)
{
if (!_profiles.ContainsKey(profileInfo))
{
_profiles.Add(profileInfo, null);
}
while (_profiles.Count > 0)
{
var first = _profiles.Keys[0];
if (first.Started < DateTime.UtcNow.Add(-CacheDuration))
{
_profiles.RemoveAt(0);
}
else
{
break;
}
}
}
if (!profiler.HasUserViewed)
{
SetUnviewed(profiler.User, profiler.Id);
}
}
/// <summary>
/// Saves <paramref name="profiler"/> to the HttpRuntime.Cache under a key concatenated with <see cref="CacheKeyPrefix"/>
/// and the parameter's <see cref="MiniProfiler.Id"/>.
/// </summary>
/// <param name="profiler">The <see cref="MiniProfiler"/> to save.</param>
public Task SaveAsync(MiniProfiler profiler)
{
Save(profiler);
return Task.CompletedTask;
}
/// <summary>
/// Set the profile to unviewed for this user
/// </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)
{
var ids = GetPerUserUnviewedIds(user);
lock (ids)
{
if (!ids.Contains(id))
{
ids.Add(id);
}
}
var key = GetPerUserUnviewedCacheKey(user);
_cache.Set(key, ids, DateTime.UtcNow + CacheDuration);
}
/// <summary>
/// Set the profile to unviewed for this user
/// </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)
{
SetUnviewed(user, id);
return Task.CompletedTask;
}
/// <summary>
/// Set the profile to viewed for this user
/// </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)
{
var ids = GetPerUserUnviewedIds(user);
lock (ids)
{
ids.Remove(id);
}
}
/// <summary>
/// Set the profile to viewed for this user
/// </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)
{
SetViewed(user, id);
return Task.CompletedTask;
}
/// <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 async Task SetViewedAsync(string user, IEnumerable<Guid> ids)
{
foreach (var id in ids)
{
await this.SetViewedAsync(user, id).ConfigureAwait(false);
}
}
}
}