LruCache.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // A simple LRU cache
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. // Andres G. Aragoneses ([email protected])
  7. //
  8. // Copyright 2010 Miguel de Icaza
  9. // Copyright 2013 7digital Media Ltd.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. namespace System.Web.Configuration {
  32. internal class LruCache<TKey, TValue> {
  33. Dictionary<TKey, LinkedListNode <TValue>> dict;
  34. Dictionary<LinkedListNode<TValue>, TKey> revdict;
  35. LinkedList<TValue> list;
  36. int entryLimit;
  37. public LruCache (int entryLimit)
  38. {
  39. this.entryLimit = entryLimit;
  40. dict = new Dictionary<TKey, LinkedListNode<TValue>> ();
  41. revdict = new Dictionary<LinkedListNode<TValue>, TKey> ();
  42. list = new LinkedList<TValue> ();
  43. }
  44. //for debugging: public int Count { get { return dict.Count; } }
  45. void Evict ()
  46. {
  47. var last = list.Last;
  48. if (last == null)
  49. return;
  50. var key = revdict [last];
  51. dict.Remove (key);
  52. revdict.Remove (last);
  53. list.RemoveLast ();
  54. DisposeValue (last.Value);
  55. }
  56. public void Clear ()
  57. {
  58. foreach (var element in list) {
  59. DisposeValue (element);
  60. }
  61. dict.Clear ();
  62. revdict.Clear ();
  63. list.Clear ();
  64. }
  65. void DisposeValue (TValue value) {
  66. if (value is IDisposable) {
  67. ((IDisposable)value).Dispose ();
  68. }
  69. }
  70. public bool TryGetValue (TKey key, out TValue value)
  71. {
  72. LinkedListNode<TValue> node;
  73. if (dict.TryGetValue (key, out node)){
  74. list.Remove (node);
  75. list.AddFirst (node);
  76. value = node.Value;
  77. return true;
  78. }
  79. value = default (TValue);
  80. return false;
  81. }
  82. public void Add (TKey key, TValue value) {
  83. LinkedListNode<TValue> node;
  84. if (dict.TryGetValue (key, out node)){
  85. // If we already have a key, move it to the front
  86. list.Remove (node);
  87. list.AddFirst (node);
  88. // Remove the old value
  89. DisposeValue (node.Value);
  90. node.Value = value;
  91. return;
  92. }
  93. if (dict.Count >= entryLimit)
  94. Evict ();
  95. // Adding new node
  96. node = new LinkedListNode<TValue> (value);
  97. list.AddFirst (node);
  98. dict [key] = node;
  99. revdict [node] = key;
  100. }
  101. public override string ToString ()
  102. {
  103. return "LRUCache dict={0} revdict={1} list={2}";
  104. }
  105. }
  106. }