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
|
//
// A simple LRU cache used for tracking the cache items
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// Copyright 2010 Miguel de Icaza
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Collections.Generic;
namespace System.Web.Caching
{
sealed class CacheItemLRU
{
public delegate bool SelectItemsQualifier (CacheItem item);
Dictionary<string, LinkedListNode <CacheItem>> dict;
Dictionary<LinkedListNode<CacheItem>, string> revdict;
LinkedList<CacheItem> list;
Cache owner;
// High/Low water mark is here to avoid situations when we hit a limit, evict an
// entry, add another one and have to evict again because the limit was hit. When we
// hit the high water limit, we evict until we reach the low water mark to avoid the
// situation.
int highWaterMark, lowWaterMark;
bool needsEviction;
public int Count {
get { return dict.Count; }
}
public CacheItemLRU (Cache owner, int highWaterMark, int lowWaterMark)
{
list = new LinkedList<CacheItem> ();
dict = new Dictionary<string, LinkedListNode<CacheItem>> (StringComparer.Ordinal);
revdict = new Dictionary<LinkedListNode<CacheItem>, string> ();
this.highWaterMark = highWaterMark;
this.lowWaterMark = lowWaterMark;
this.owner = owner;
}
public bool TryGetValue (string key, out CacheItem value)
{
LinkedListNode <CacheItem> item;
if (dict.TryGetValue (key, out item)) {
value = item.Value;
return true;
}
value = null;
return false;
}
// Must ALWAYS be called with the owner's write lock held
public void EvictIfNecessary ()
{
if (!needsEviction)
return;
for (int i = dict.Count; i > lowWaterMark; i--) {
var key = revdict [list.Last];
owner.Remove (key, CacheItemRemovedReason.Underused, false, true);
}
}
// Must ALWAYS be called with the owner's read lock held
public void InvokePrivateCallbacks ()
{
foreach (var de in dict) {
CacheItem item = de.Value.Value;
if (item == null || item.Disabled)
continue;
if (item.OnRemoveCallback != null) {
try {
item.OnRemoveCallback (de.Key, item.Value, CacheItemRemovedReason.Removed);
} catch {
//TODO: anything to be done here?
}
}
}
}
// Must ALWAYS be called with the owner's write lock held
public List <CacheItem> SelectItems (SelectItemsQualifier qualifier)
{
var ret = new List <CacheItem> ();
foreach (LinkedListNode <CacheItem> node in dict.Values) {
CacheItem item = node.Value;
if (qualifier (item))
ret.Add (item);
}
return ret;
}
// Must ALWAYS be called with the owner's read lock held
public List <CacheItem> ToList ()
{
var ret = new List <CacheItem> ();
if (dict.Count == 0)
return ret;
foreach (LinkedListNode <CacheItem> node in dict.Values)
ret.Add (node.Value);
return ret;
}
public void Remove (string key)
{
if (key == null)
return;
LinkedListNode <CacheItem> node;
if (!dict.TryGetValue (key, out node))
return;
CacheItem item = node.Value;
dict.Remove (key);
if (item == null || item.Priority != CacheItemPriority.NotRemovable) {
revdict.Remove (node);
list.Remove (node);
}
}
public CacheItem this [string key] {
get {
if (key == null)
return null;
LinkedListNode<CacheItem> node;
CacheItem item;
if (dict.TryGetValue (key, out node)){
item = node.Value;
if (item == null || item.Priority != CacheItemPriority.NotRemovable) {
list.Remove (node);
list.AddFirst (node);
}
return item;
}
return null;
}
set {
LinkedListNode<CacheItem> node;
if (dict.TryGetValue (key, out node)){
// If we already have a key, move it to the front
list.Remove (node);
if (value == null || value.Priority != CacheItemPriority.NotRemovable)
list.AddFirst (node);
else
revdict.Remove (node);
node.Value = value;
return;
}
needsEviction = dict.Count >= highWaterMark;
// Adding new node
node = new LinkedListNode<CacheItem> (value);
if (value == null || value.Priority != CacheItemPriority.NotRemovable) {
list.AddFirst (node);
revdict [node] = key;
}
dict [key] = node;
}
}
}
}
|