Repository navigation
Expand file tree
/
Copy pathEventCache.cs
More file actions
56 lines (50 loc) · 1.95 KB
/
Copy pathEventCache.cs
File metadata and controls
56 lines (50 loc) · 1.95 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
namespace EventFast;
internal sealed class EventCache
{
private readonly Dictionary<string, Entry> _entries = [];
private readonly long _limit = Math.Clamp(GC.GetGCMemoryInfo().TotalAvailableMemoryBytes * 8 / 100, 256L << 20, 1L << 30);
private readonly object _gate = new();
private long _size;
internal IReadOnlyList<EventRow> GetOrAdd(string key, Func<IReadOnlyList<EventRow>> factory, bool bypassCache = false)
{
lock (_gate)
{
if (!bypassCache && _entries.TryGetValue(key, out var cached) && DateTime.UtcNow - cached.Created < TimeSpan.FromMinutes(2))
return cached.Rows;
}
var rows = factory();
var entry = new Entry(rows, rows.Sum(Estimate), DateTime.UtcNow);
lock (_gate)
{
if (_entries.TryGetValue(key, out var previous))
_size -= previous.Size;
_entries[key] = entry;
_size += entry.Size;
// ponytail: clear-all eviction is enough for a two-channel cache; use LRU only when more channels land.
if (_size > _limit)
{
_entries.Clear();
_size = 0;
}
}
return rows;
}
private static long Estimate(EventRow row) =>
128L + (row.Provider.Length + row.Channel.Length + row.Computer.Length + row.Details.Length + row.Xml.Length + (row.Message?.Length ?? 0)) * sizeof(char);
internal static void SelfTest()
{
var cache = new EventCache();
var calls = 0;
IReadOnlyList<EventRow> Factory()
{
calls++;
return [];
}
cache.GetOrAdd("x", Factory);
cache.GetOrAdd("x", Factory);
cache.GetOrAdd("x", Factory, bypassCache: true);
if (calls != 2)
throw new InvalidOperationException("Event cache self-test failed.");
}
private sealed record Entry(IReadOnlyList<EventRow> Rows, long Size, DateTime Created);
}