/
LRUCache.cs
148 lines (133 loc) · 5.18 KB
/
LRUCache.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
using System;
using System.Collections.Generic;
namespace Reusables.Caching.InMemory
{
/// <summary>
/// A least-recently-used cache stored like a dictionary.
/// </summary>
/// <typeparam name="TKey">The type of the key to the cached item.</typeparam>
/// <typeparam name="TValue">The type of the cached item.</typeparam>
/// <remarks>
/// Code was taken from Microsoft Bot Framework SDK: https://github.com/microsoft/botbuilder-dotnet.
/// Class link: https://github.com/microsoft/botbuilder-dotnet/blob/402bc02b4cbbd2f4ec359134640e99211367e4a5/libraries/AdaptiveExpressions/LRUCache.cs
/// </remarks>
public sealed class LRUCache<TKey, TValue>
{
/// <summary>
/// Default maximum number of elements to cache.
/// </summary>
private const int DefaultCapacity = 255;
private static readonly TimeSpan DefaultTTL = TimeSpan.FromMinutes(2);
private readonly object _lockObj = new object();
private readonly int _capacity;
private readonly TimeSpan _ttl;
private readonly Dictionary<TKey, Entry> _cacheMap;
private readonly LinkedList<TKey> _cacheList;
/// <summary>
/// Initializes a new instance of the <see cref="LRUCache{TKey, TValue}"/> class.
/// </summary>
public LRUCache()
: this(DefaultCapacity, DefaultTTL)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LRUCache{TKey, TValue}"/> class.
/// </summary>
/// <param name="capacity">Maximum number of elements to cache.</param>
public LRUCache(int capacity, TimeSpan ttl)
{
_capacity = capacity > 0 ? capacity : DefaultCapacity;
_ttl = ttl;
_cacheMap = new Dictionary<TKey, Entry>(_capacity);
_cacheList = new LinkedList<TKey>();
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="value">When this method returns, contains the value associated with
/// the specified key, if the key is found; otherwise, the default value for the
/// type of the <paramref name="value" /> parameter.</param>
/// <returns>true if contains an element with the specified key; otherwise, false.</returns>
public bool TryGet(TKey key, out TValue value)
{
LRUCacheEventSource.Log.ItemRequested();
lock (_lockObj)
{
if (_cacheMap.TryGetValue(key, out var entry))
{
if ((DateTime.Now - entry.DateAdded).TotalMilliseconds > _ttl.TotalMilliseconds)
{
_cacheList.Remove(entry.Node);
_cacheMap.Remove(key);
LRUCacheEventSource.Log.ItemExpired();
value = default;
return false;
}
Touch(entry.Node);
value = entry.Value;
LRUCacheEventSource.Log.ItemFetched();
return true;
}
}
value = default;
LRUCacheEventSource.Log.ItemNotFound();
return false;
}
/// <summary>
/// Adds the specified key and value to the cache.
/// </summary>
/// <param name="key">The key of the element to add.</param>
/// <param name="value">The value of the element to add.</param>
public void Set(TKey key, TValue value)
{
lock (_lockObj)
{
if (!_cacheMap.TryGetValue(key, out var entry))
{
LinkedListNode<TKey> node;
if (_cacheMap.Count >= _capacity)
{
node = _cacheList.Last;
_cacheMap.Remove(node.Value);
_cacheList.RemoveLast();
node.Value = key;
}
else
{
node = new LinkedListNode<TKey>(key);
}
_cacheList.AddFirst(node);
_cacheMap.Add(key, new Entry(node, value, DateTime.Now));
LRUCacheEventSource.Log.ItemAdded();
}
else
{
entry.Value = value;
_cacheMap[key] = entry;
Touch(entry.Node);
}
}
}
private void Touch(LinkedListNode<TKey> node)
{
if (node != _cacheList.First)
{
_cacheList.Remove(node);
_cacheList.AddFirst(node);
}
}
private struct Entry
{
public LinkedListNode<TKey> Node;
public TValue Value;
public DateTime DateAdded;
public Entry(LinkedListNode<TKey> node, TValue value, DateTime dateAdded)
{
Node = node;
Value = value;
DateAdded = dateAdded;
}
}
}
}