CacheItemPriorityQueueDebug.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Author(s):
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Xml;
  34. namespace System.Web.Caching
  35. {
  36. enum EDSequenceEntryType
  37. {
  38. Enqueue,
  39. Dequeue,
  40. Disable,
  41. Peek,
  42. Update
  43. }
  44. #if DEBUG
  45. [Serializable]
  46. sealed class EDSequenceEntry
  47. {
  48. public readonly EDSequenceEntryType Type;
  49. public readonly CacheItem Item;
  50. public EDSequenceEntry ()
  51. {}
  52. public EDSequenceEntry (CacheItem item, EDSequenceEntryType type)
  53. {
  54. Type = type;
  55. Item = item;
  56. }
  57. }
  58. #endif
  59. sealed partial class CacheItemPriorityQueue
  60. {
  61. #if DEBUG
  62. Dictionary <Guid, CacheItem> items = new Dictionary <Guid, CacheItem> ();
  63. List <EDSequenceEntry> edSequence = new List <EDSequenceEntry> ();
  64. List <EDSequenceEntry> EDSequence {
  65. get {
  66. if (edSequence == null)
  67. edSequence = new List <EDSequenceEntry> ();
  68. return edSequence;
  69. }
  70. }
  71. #endif
  72. [Conditional ("DEBUG")]
  73. void InitDebugMode ()
  74. {
  75. #if DEBUG
  76. AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
  77. #endif
  78. }
  79. [Conditional ("DEBUG")]
  80. void AddSequenceEntry (CacheItem item, EDSequenceEntryType type)
  81. {
  82. #if DEBUG
  83. EDSequence.Add (new EDSequenceEntry (CopyItem (item), type));
  84. #endif
  85. }
  86. #if DEBUG
  87. CacheItem CopyItem (CacheItem item)
  88. {
  89. CacheItem newItem = new CacheItem ();
  90. // if (items.TryGetValue (item.Guid, out newItem)) {
  91. // newItem.PriorityQueueIndex = item.PriorityQueueIndex
  92. // }
  93. newItem.Key = item.Key;
  94. newItem.AbsoluteExpiration = item.AbsoluteExpiration;
  95. newItem.SlidingExpiration = item.SlidingExpiration;
  96. newItem.Priority = item.Priority;
  97. newItem.LastChange = item.LastChange;
  98. newItem.ExpiresAt = item.ExpiresAt;
  99. newItem.Disabled = item.Disabled;
  100. newItem.PriorityQueueIndex = item.PriorityQueueIndex;
  101. newItem.Guid = item.Guid;
  102. //items [newItem.Guid] = newItem;
  103. return newItem;
  104. }
  105. string CreateNewCacheItemInstanceCode (string indent, CacheItem item)
  106. {
  107. var sb = new StringBuilder (indent + "new CacheItem {");
  108. sb.AppendFormat ("Key = \"{0}\", ", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r"));
  109. sb.AppendFormat ("AbsoluteExpiration = DateTime.Parse (\"{0}\"), ", item.AbsoluteExpiration.ToString ());
  110. sb.AppendFormat ("SlidingExpiration = TimeSpan.Parse (\"{0}\"), ", item.SlidingExpiration.ToString ());
  111. sb.AppendFormat ("Priority = CacheItemPriority.{0}, ", item.Priority);
  112. sb.AppendFormat ("LastChange = DateTime.Parse (\"{0}\"), ", item.LastChange.ToString ());
  113. sb.AppendFormat ("ExpiresAt = {0}, ", item.ExpiresAt);
  114. sb.AppendFormat ("Disabled = {0}, ", item.Disabled.ToString ().ToLowerInvariant ());
  115. sb.AppendFormat ("PriorityQueueIndex = {0},", item.PriorityQueueIndex);
  116. sb.AppendFormat ("Guid = new Guid (\"{0}\")}}, \n", item.Guid.ToString ());
  117. return sb.ToString ();
  118. }
  119. #endif
  120. [Conditional ("DEBUG")]
  121. public void OnItemDisable (CacheItem i)
  122. {
  123. #if DEBUG
  124. CacheItem item;
  125. if (!items.TryGetValue (i.Guid, out item))
  126. return;
  127. EDSequence.Add (new EDSequenceEntry (CopyItem (i), EDSequenceEntryType.Disable));
  128. #endif
  129. }
  130. #if DEBUG
  131. void OnDomainUnload (object sender, EventArgs e)
  132. {
  133. if (EDSequence.Count == 0) {
  134. Console.Error.WriteLine ("No enqueue/dequeue sequence recorded.");
  135. return;
  136. }
  137. try {
  138. string filePath = Path.Combine (HttpRuntime.AppDomainAppPath, String.Format ("cache_pq_sequence_{0}_{1:x}.seq",
  139. DateTime.UtcNow.ToString ("yyyy-MM-dd_hh-mm-ss"),
  140. GetHashCode ()));
  141. var settings = new XmlWriterSettings ();
  142. settings.Indent = true;
  143. settings.IndentChars = "\t";
  144. settings.NewLineChars = "\n";
  145. settings.Encoding = Encoding.UTF8;
  146. Console.Error.WriteLine ("Saving sequence in file {0}", filePath);
  147. using (XmlWriter writer = XmlWriter.Create (filePath, settings)) {
  148. writer.WriteStartDocument (true);
  149. writer.WriteStartElement ("sequence");
  150. foreach (EDSequenceEntry entry in EDSequence) {
  151. writer.WriteStartElement ("entry");
  152. writer.WriteAttributeString ("type", entry.Type.ToString ());
  153. if (entry.Item == null)
  154. writer.WriteAttributeString ("isNull", "true");
  155. else {
  156. writer.WriteAttributeString ("key", entry.Item.Key != null ? entry.Item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r") : "null");
  157. writer.WriteAttributeString ("absoluteExpiration", entry.Item.AbsoluteExpiration.Ticks.ToString ());
  158. writer.WriteAttributeString ("slidingExpiration", entry.Item.SlidingExpiration.Ticks.ToString ());
  159. writer.WriteAttributeString ("priority", entry.Item.Priority.ToString ());
  160. writer.WriteAttributeString ("lastChange", entry.Item.LastChange.Ticks.ToString ());
  161. writer.WriteAttributeString ("expiresAt", entry.Item.ExpiresAt.ToString ());
  162. writer.WriteAttributeString ("disabled", entry.Item.Disabled.ToString ());
  163. writer.WriteAttributeString ("guid", entry.Item.Guid.ToString ());
  164. writer.WriteAttributeString ("priorityQueueIndex", entry.Item.PriorityQueueIndex.ToString ());
  165. }
  166. writer.WriteEndElement ();
  167. }
  168. writer.WriteEndElement ();
  169. writer.Flush ();
  170. }
  171. } catch (Exception ex) {
  172. Console.WriteLine ("Failed to write enqueue/dequeue sequence file. Exception was caught:\n{0}",
  173. ex);
  174. }
  175. }
  176. #endif
  177. }
  178. }