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
|
//
// A simple LRU cache
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
// Andres G. Aragoneses (andres@7digital.com)
//
// Copyright 2010 Miguel de Icaza
// Copyright 2013 7digital Media Ltd.
//
// 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.Configuration {
class LruCache<TKey, TValue> {
Dictionary<TKey, LinkedListNode <TValue>> dict;
Dictionary<LinkedListNode<TValue>, TKey> revdict;
LinkedList<TValue> list;
int entry_limit;
bool eviction_warning_shown;
int evictions;
internal string EvictionWarning { set; private get; }
public LruCache (int entryLimit)
{
entry_limit = entryLimit;
dict = new Dictionary<TKey, LinkedListNode<TValue>> ();
revdict = new Dictionary<LinkedListNode<TValue>, TKey> ();
list = new LinkedList<TValue> ();
}
//for debugging: public int Count { get { return dict.Count; } }
void Evict ()
{
var last = list.Last;
if (last == null)
return;
var key = revdict [last];
dict.Remove (key);
revdict.Remove (last);
list.RemoveLast ();
DisposeValue (last.Value);
evictions++;
if (!String.IsNullOrEmpty (EvictionWarning) && !eviction_warning_shown && (evictions >= entry_limit)) {
Console.Error.WriteLine ("WARNING: " + EvictionWarning);
eviction_warning_shown = true;
}
}
public void Clear ()
{
foreach (var element in list) {
DisposeValue (element);
}
dict.Clear ();
revdict.Clear ();
list.Clear ();
eviction_warning_shown = false;
evictions = 0;
}
void DisposeValue (TValue value)
{
if (value is IDisposable) {
((IDisposable)value).Dispose ();
}
}
public bool TryGetValue (TKey key, out TValue value)
{
LinkedListNode<TValue> node;
if (dict.TryGetValue (key, out node)){
list.Remove (node);
list.AddFirst (node);
value = node.Value;
return true;
}
value = default (TValue);
return false;
}
public void Add (TKey key, TValue value)
{
LinkedListNode<TValue> node;
if (dict.TryGetValue (key, out node)){
// If we already have a key, move it to the front
list.Remove (node);
list.AddFirst (node);
// Remove the old value
DisposeValue (node.Value);
node.Value = value;
return;
}
if (dict.Count >= entry_limit)
Evict ();
// Adding new node
node = new LinkedListNode<TValue> (value);
list.AddFirst (node);
dict [key] = node;
revdict [node] = key;
}
public override string ToString ()
{
return "LRUCache dict={0} revdict={1} list={2}";
}
}
}
|