-
-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathInMemoryStorage.cs
More file actions
227 lines (191 loc) · 5.91 KB
/
InMemoryStorage.cs
File metadata and controls
227 lines (191 loc) · 5.91 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.CompilerServices;
namespace StabilityMatrix.Avalonia.Controls.VendorLabs.Cache;
/// <summary>
/// Generic in-memory storage of items
/// </summary>
/// <typeparam name="T">T defines the type of item stored</typeparam>
public class InMemoryStorage<T>
{
private readonly Dictionary<string, LinkedListNode<InMemoryStorageItem<T>>> _inMemoryStorage = new();
private readonly LinkedList<InMemoryStorageItem<T>> _lruList = [];
private int _maxItemCount;
private readonly Lock _settingMaxItemCountLocker = new();
/// <summary>
/// Gets or sets the maximum count of Items that can be stored in this InMemoryStorage instance.
/// </summary>
public int MaxItemCount
{
get => _maxItemCount;
set
{
if (_maxItemCount == value)
{
return;
}
_maxItemCount = value;
lock (_settingMaxItemCountLocker)
{
EnsureStorageBounds(value);
}
}
}
public int Count => _inMemoryStorage.Count;
/// <summary>
/// Clears all items stored in memory
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Clear()
{
_inMemoryStorage.Clear();
_lruList.Clear();
}
/// <summary>
/// Clears items stored in memory based on duration passed
/// </summary>
/// <param name="duration">TimeSpan to identify expired items</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Clear(TimeSpan duration)
{
Clear(DateTime.Now.Subtract(duration));
}
/// <summary>
/// Clears items stored in memory based on duration passed
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Clear(DateTime expirationDate)
{
foreach (var (key, node) in _inMemoryStorage)
{
if (string.IsNullOrWhiteSpace(key))
{
continue;
}
var item = node.Value;
if (item.LastUpdated > expirationDate)
{
continue;
}
Remove(key);
}
}
/// <summary>
/// Remove items based on provided keys
/// </summary>
/// <param name="keys">identified of the in-memory storage item</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Remove(IEnumerable<string> keys)
{
foreach (var key in keys)
{
if (string.IsNullOrWhiteSpace(key))
{
continue;
}
Remove(key);
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Remove(string key)
{
if (!_inMemoryStorage.TryGetValue(key, out var node))
return;
_lruList.Remove(node);
_inMemoryStorage.Remove(key);
}
/// <summary>
/// Add new item to in-memory storage
/// </summary>
/// <param name="item">item to be stored</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetItem(InMemoryStorageItem<T> item)
{
if (MaxItemCount == 0)
{
return;
}
if (_inMemoryStorage.TryGetValue(item.Id, out var node))
{
_lruList.Remove(node);
}
else if (_inMemoryStorage.Count >= MaxItemCount)
{
RemoveFirst();
}
var newNode = new LinkedListNode<InMemoryStorageItem<T>>(item);
_lruList.AddLast(newNode);
_inMemoryStorage[item.Id] = newNode;
/*// ensure max limit is maintained. trim older entries first
if (_inMemoryStorage.Count > MaxItemCount)
{
var itemsToRemove = _inMemoryStorage
.OrderBy(kvp => kvp.Value.Created)
.Take(_inMemoryStorage.Count - MaxItemCount)
.Select(kvp => kvp.Key);
Remove(itemsToRemove);
}*/
}
/// <summary>
/// Get item from in-memory storage as long as it has not ex
/// </summary>
/// <param name="id">id of the in-memory storage item</param>
/// <param name="duration">timespan denoting expiration</param>
/// <returns>Valid item if not out of date or return null if out of date or item does not exist</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public InMemoryStorageItem<T>? GetItem(string id, TimeSpan duration)
{
if (!_inMemoryStorage.TryGetValue(id, out var node))
{
return null;
}
var expirationDate = DateTime.Now.Subtract(duration);
if (node.Value.LastUpdated <= expirationDate)
{
Remove(id);
return null;
}
_lruList.Remove(node);
_lruList.AddLast(node);
return node.Value;
}
[MethodImpl(MethodImplOptions.Synchronized)]
public InMemoryStorageItem<T>? GetItem(string id)
{
if (!_inMemoryStorage.TryGetValue(id, out var node))
{
return null;
}
var value = node.Value;
_lruList.Remove(node);
_lruList.AddLast(node);
return value;
}
private void RemoveFirst()
{
// Remove from LRUPriority
var node = _lruList.First;
_lruList.RemoveFirst();
if (node == null)
return;
// Remove from cache
_inMemoryStorage.Remove(node.Value.Id);
}
private void EnsureStorageBounds(int maxCount)
{
if (_inMemoryStorage.Count == 0)
{
return;
}
if (maxCount == 0)
{
_inMemoryStorage.Clear();
return;
}
if (_inMemoryStorage.Count > maxCount)
{
Remove(_inMemoryStorage.Keys.Take(_inMemoryStorage.Count - maxCount));
}
}
}