MemoryCacheLRU.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // MemoryCacheLRU.cs
  3. //
  4. // Authors:
  5. // Marek Habersack <[email protected]>
  6. //
  7. // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. namespace System.Runtime.Caching
  31. {
  32. // NOTE: all the public methods in this assume that the owner's write lock is held
  33. sealed class MemoryCacheLRU
  34. {
  35. // int trimLowerBound;
  36. Dictionary <int, LinkedListNode <MemoryCacheEntry>> index;
  37. LinkedList <MemoryCacheEntry> lru;
  38. MemoryCacheContainer owner;
  39. public MemoryCacheLRU (MemoryCacheContainer owner, int trimLowerBound)
  40. {
  41. // this.trimLowerBound = trimLowerBound;
  42. index = new Dictionary <int, LinkedListNode <MemoryCacheEntry>> ();
  43. lru = new LinkedList <MemoryCacheEntry> ();
  44. this.owner = owner;
  45. }
  46. public void Update (MemoryCacheEntry entry)
  47. {
  48. if (entry == null)
  49. return;
  50. int hash = entry.GetHashCode ();
  51. LinkedListNode <MemoryCacheEntry> node;
  52. if (!index.TryGetValue (hash, out node)) {
  53. node = new LinkedListNode <MemoryCacheEntry> (entry);
  54. index.Add (hash, node);
  55. } else {
  56. lru.Remove (node);
  57. node.Value = entry;
  58. }
  59. lru.AddLast (node);
  60. }
  61. public void Remove (MemoryCacheEntry entry)
  62. {
  63. if (entry == null)
  64. return;
  65. int hash = entry.GetHashCode ();
  66. LinkedListNode <MemoryCacheEntry> node;
  67. if (index.TryGetValue (hash, out node)) {
  68. lru.Remove (node);
  69. index.Remove (hash);
  70. }
  71. }
  72. public long Trim (long upTo)
  73. {
  74. int count = index.Count;
  75. if (count <= 10)
  76. return 0;
  77. // The list is used below to reproduce .NET's behavior which selects the
  78. // entries using the LRU order, but it removes them from the cache in the
  79. // MRU order
  80. var toremove = new List <MemoryCacheEntry> ((int)upTo);
  81. long removed = 0;
  82. MemoryCacheEntry entry;
  83. while (upTo > removed && count > 10) {
  84. entry = lru.First.Value;
  85. toremove.Insert (0, entry);
  86. Remove (entry);
  87. removed++;
  88. count--;
  89. }
  90. foreach (MemoryCacheEntry e in toremove)
  91. owner.Remove (e.Key, false);
  92. return removed;
  93. }
  94. }
  95. }