-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cs
184 lines (172 loc) · 4.71 KB
/
Cache.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
using System.Collections.Generic;
namespace PiGameSharp
{
/// <summary>
/// A resource cache using the Adaptive Replacement Cache (ARC) design.
/// </summary>
public class Cache
{
// TODO: Come up with better locking, it's rather heavy handed right now.
private object sync_key = new object();
private LinkedList<Resource> MRU_ghost = new LinkedList<Resource>();
private LinkedList<Resource> MRU = new LinkedList<Resource>();
private LinkedList<Resource> MFU = new LinkedList<Resource>();
private LinkedList<Resource> MFU_ghost = new LinkedList<Resource>();
private int cache = 100;
private int split = 30;
/// <summary>
/// Gets or sets the size of the cache.
/// </summary>
/// <remarks>
/// This value sets the number of items kept loaded in the cache. Items that become unloaded are still tracked by the cache in ghost lists. The total number of items tracked by the cache is 2 * CacheSize.
///
/// If the cache size is reduced below the number of items currently kept in the cache, items are first eviced from the Most Recently Used side, after that the Most Frequently Used side.
/// </remarks>
public int CacheSize
{
get => cache;
set
{
if (value < 0)
value = 0;
lock (sync_key)
{
while (value < cache)
{
if (split > 1)
{
Evict(MRU, MRU_ghost);
split--;
}
else
Evict(MFU, MFU_ghost);
cache--;
}
if (value > cache)
split += (value - cache) / 2;
cache = value;
while (MRU_ghost.Count > (cache >> 1))
Evict(MRU_ghost, null);
while (MFU_ghost.Count > (cache >> 1))
Evict(MFU_ghost, null);
}
}
}
/// <summary>
/// Adds an item to the cache, evicting older or less frequently used resources if needed.
/// </summary>
/// <param name="r">The resource to add</param>
public void Add(Resource r)
{
lock (sync_key)
{
foreach (LinkedListNode<Resource> item in GetItems())
if (item.Value.Key == r.Key)
return;
AddMRU(r);
}
r.LoadSync();
}
/// <summary>
/// Finds a resource in the cache
/// </summary>
/// <param name="key">The key of the resource to lookup</param>
/// <returns>The requested resource if found, null otherwise</returns>
public Resource Lookup(int key)
{
lock (sync_key)
{
foreach (LinkedListNode<Resource> item in GetItems())
if (item.Value.Key == key)
{
if (item.List == MFU_ghost)
{
if (split > 1)
split--;
}
else if (item.List == MRU_ghost)
{
if (split < cache - 1)
split++;
}
item.List.Remove(item);
AddMFU(item.Value);
return item.Value;
}
}
return null;
}
/// <summary>
/// Flushes a specific entry from the cache
/// </summary>
/// <param name="key">The resource to flush</param>
/// <remarks>This operation causes the flushed resource to become unloaded</remarks>
public void Flush(int key)
{
lock (sync_key)
{
foreach (LinkedListNode<Resource> item in GetItems())
if (item.Value.Key == key)
{
item.Value.UnloadSync();
item.List.Remove(item);
break;
}
}
}
/// <summary>
/// Flushes the entire cache
/// </summary>
/// <remarks>This operation causes the flushed resources to become unloaded</remarks>
public void Flush()
{
lock (sync_key)
{
foreach (LinkedListNode<Resource> item in GetItems())
item.Value.UnloadSync();
MFU.Clear();
MRU.Clear();
MFU_ghost.Clear();
MRU_ghost.Clear();
}
}
private void AddMRU(Resource r)
{
MRU.AddFirst(r);
while (MRU.Count >= split)
Evict(MRU, MRU_ghost);
}
private void AddMFU(Resource r)
{
MFU.AddFirst(r);
while (MFU.Count >= (cache - split))
Evict(MFU, MFU_ghost);
}
private void Evict(LinkedList<Resource> list, LinkedList<Resource> list_ghost)
{
Resource item = list.Last.Value;
list.RemoveLast();
if (list_ghost != null)
{
item.UnloadSync();
list_ghost.AddFirst(item);
while (list_ghost.Count > (cache >> 1))
Evict(list_ghost, null);
}
}
private IEnumerable<LinkedListNode<Resource>> GetItems()
{
LinkedListNode<Resource> finger;
foreach (LinkedList<Resource> list in new LinkedList<Resource>[] { MFU, MRU, MFU_ghost, MRU_ghost })
{
finger = list.First;
while (finger != null)
{
LinkedListNode<Resource> next = finger.Next; // allow list modifications (only item finger) outside of the enumerator by getting the next element first.
yield return finger;
finger = next;
}
}
}
}
}