-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLruCache.cs
More file actions
69 lines (62 loc) · 1.41 KB
/
LruCache.cs
File metadata and controls
69 lines (62 loc) · 1.41 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
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var cache = new LruCache<int,int>(getNum, 7);
for (int i = 0; i < 10; i++)
Console.Write(cache.GetItem(i) + " ");
for (int i = 9; i >= 0; i--)
Console.Write(cache.GetItem(i) + " ");
for (int i = 0; i < 10; i++)
Console.Write(cache.GetItem(i) + " ");
Console.WriteLine();
}
static int getNum(int num)
{
Console.Write("X"); //signifies cache miss
return num;
}
}
class LruCache<TKey, TVal>
{
int _cacheSize;
Func<TKey, TVal> _getUncached;
Dictionary<TKey, TVal> _cache;
List<Tuple<TKey, DateTime>> _accessTimes;
public LruCache(Func<TKey, TVal> getUncached, int cacheSize)
{
_cacheSize = cacheSize;
_getUncached = getUncached;
Clear();
}
public void Clear()
{
_cache = new Dictionary<TKey, TVal>();
_accessTimes = new List<Tuple<TKey, DateTime>>();
}
private void trimCache()
{
if(_cache.Count > _cacheSize)
{
_accessTimes.Sort((y,x) => x.Item2.CompareTo(y.Item2));
var leastUsed = _accessTimes[_accessTimes.Count - 1];
_cache.Remove(leastUsed.Item1);
_accessTimes.Remove(leastUsed);
}
}
public TVal GetItem(TKey key)
{
TVal item;
if(!_cache.TryGetValue(key, out item))
{
item = _getUncached(key);
_cache.Add(key, item);
}
_accessTimes.RemoveAll(x => x.Item1.Equals(key));
_accessTimes.Add(Tuple.Create(key, DateTime.Now));
trimCache();
return item;
}
}