-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathConcurrentLruCache.cs
More file actions
103 lines (84 loc) · 2.43 KB
/
ConcurrentLruCache.cs
File metadata and controls
103 lines (84 loc) · 2.43 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Python.Runtime;
internal sealed class ConcurrentLruCache<TKey, TValue> where TKey : notnull
{
readonly ConcurrentDictionary<TKey, LinkedListNode<CacheItem>> map = new();
readonly LinkedList<CacheItem> lru = new();
readonly object gate = new();
sealed record CacheItem(TKey Key, TValue Value);
public ConcurrentLruCache(int capacity)
{
if (capacity <= 0)
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
Capacity = capacity;
}
public int Capacity { get; private set; }
public int Count => map.Count;
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
if (valueFactory is null)
throw new ArgumentNullException(nameof(valueFactory));
if (TryGetValue(key, out var existing))
return existing;
var created = valueFactory(key);
lock (gate)
{
if (map.TryGetValue(key, out var alreadyAdded))
{
MoveToFront(alreadyAdded);
return alreadyAdded.Value.Value;
}
var item = new CacheItem(key, created);
var node = new LinkedListNode<CacheItem>(item);
lru.AddFirst(node);
map[key] = node;
EvictOverflow();
return created;
}
}
public bool TryGetValue(TKey key, out TValue value)
{
if (map.TryGetValue(key, out var node))
{
lock (gate)
{
if (map.TryGetValue(key, out node))
{
MoveToFront(node);
value = node.Value.Value;
return true;
}
}
}
value = default!;
return false;
}
public void Clear()
{
lock (gate)
{
lru.Clear();
map.Clear();
}
}
void MoveToFront(LinkedListNode<CacheItem> node)
{
if (ReferenceEquals(lru.First, node))
return;
lru.Remove(node);
lru.AddFirst(node);
}
void EvictOverflow()
{
while (map.Count > Capacity)
{
var last = lru.Last;
if (last is null)
return;
lru.RemoveLast();
map.TryRemove(last.Value.Key, out _);
}
}
}