-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ConcurrentLruCache.cs
205 lines (184 loc) · 5.75 KB
/
ConcurrentLruCache.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
namespace Microsoft.NET.Sdk.Razor.Tool
{
/// <summary>
/// Cache with a fixed size that evicts the least recently used members.
/// Thread-safe.
/// This was taken from https://github.com/dotnet/roslyn/blob/749c0ec135d7d080658dc1aa794d15229c3d10d2/src/Compilers/Core/Portable/InternalUtilities/ConcurrentLruCache.cs.
/// </summary>
internal class ConcurrentLruCache<TKey, TValue>
{
private readonly int _capacity;
private readonly Dictionary<TKey, CacheValue> _cache;
private readonly LinkedList<TKey> _nodeList;
// This is a naive course-grained lock, it can probably be optimized
private readonly object _lockObject = new object();
public ConcurrentLruCache(int capacity)
: this (capacity, EqualityComparer<TKey>.Default)
{
}
public ConcurrentLruCache(int capacity, IEqualityComparer<TKey> comparer)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
_capacity = capacity;
_cache = new Dictionary<TKey, CacheValue>(capacity, comparer);
_nodeList = new LinkedList<TKey>();
}
/// <summary>
/// Create cache from an array. The cache capacity will be the size
/// of the array. All elements of the array will be added to the
/// cache. If any duplicate keys are found in the array a
/// <see cref="ArgumentException"/> will be thrown.
/// </summary>
public ConcurrentLruCache(KeyValuePair<TKey, TValue>[] array)
: this(array.Length)
{
foreach (var kvp in array)
{
UnsafeAdd(kvp.Key, kvp.Value);
}
}
public int Count
{
get
{
lock (_lockObject)
{
return _cache.Count;
}
}
}
public void Add(TKey key, TValue value)
{
lock (_lockObject)
{
UnsafeAdd(key, value);
}
}
public TValue GetOrAdd(TKey key, TValue value)
{
lock (_lockObject)
{
if (UnsafeTryGetValue(key, out var result))
{
return result;
}
else
{
UnsafeAdd(key, value);
return value;
}
}
}
public bool TryGetValue(TKey key, out TValue value)
{
lock (_lockObject)
{
return UnsafeTryGetValue(key, out value);
}
}
public bool Remove(TKey key)
{
lock (_lockObject)
{
return UnsafeRemove(key);
}
}
/// <summary>
/// For testing. Very expensive.
/// </summary>
internal IEnumerable<KeyValuePair<TKey, TValue>> TestingEnumerable
{
get
{
lock (_lockObject)
{
foreach (var key in _nodeList)
{
var kvp = new KeyValuePair<TKey, TValue>(key, _cache[key].Value);
yield return kvp;
}
}
}
}
/// <summary>
/// Doesn't lock.
/// </summary>
private bool UnsafeTryGetValue(TKey key, out TValue value)
{
if (_cache.TryGetValue(key, out var result))
{
MoveNodeToTop(result.Node);
value = result.Value;
return true;
}
else
{
value = default(TValue);
return false;
}
}
private void MoveNodeToTop(LinkedListNode<TKey> node)
{
if (!object.ReferenceEquals(_nodeList.First, node))
{
_nodeList.Remove(node);
_nodeList.AddFirst(node);
}
}
/// <summary>
/// Expects non-empty cache. Does not lock.
/// </summary>
private void UnsafeEvictLastNode()
{
Debug.Assert(_capacity > 0);
var lastNode = _nodeList.Last;
_nodeList.Remove(lastNode);
_cache.Remove(lastNode.Value);
}
private void UnsafeAddNodeToTop(TKey key, TValue value)
{
var node = new LinkedListNode<TKey>(key);
_cache.Add(key, new CacheValue(value, node));
_nodeList.AddFirst(node);
}
/// <summary>
/// Doesn't lock.
/// </summary>
private void UnsafeAdd(TKey key, TValue value)
{
if (_cache.TryGetValue(key, out var result))
{
throw new ArgumentException("Key already exists", nameof(key));
}
else
{
if (_cache.Count == _capacity)
{
UnsafeEvictLastNode();
}
UnsafeAddNodeToTop(key, value);
}
}
private bool UnsafeRemove(TKey key)
{
_nodeList.Remove(key);
return _cache.Remove(key);
}
private struct CacheValue
{
public CacheValue(TValue value, LinkedListNode<TKey> node)
{
Value = value;
Node = node;
}
public TValue Value { get; }
public LinkedListNode<TKey> Node { get; }
}
}
}