CacheItemPriorityQueueDebug.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  43. #if DEBUG
  44. [Serializable]
  45. sealed class EDSequenceEntry
  46. {
  47. public readonly EDSequenceEntryType Type;
  48. public readonly CacheItem Item;
  49. public EDSequenceEntry ()
  50. {}
  51. public EDSequenceEntry (CacheItem item, EDSequenceEntryType type)
  52. {
  53. Type = type;
  54. Item = item;
  55. }
  56. }
  57. #endif
  58. sealed partial class CacheItemPriorityQueue
  59. {
  60. #if DEBUG
  61. Dictionary <Guid, CacheItem> items = new Dictionary <Guid, CacheItem> ();
  62. List <EDSequenceEntry> edSequence = new List <EDSequenceEntry> ();
  63. List <EDSequenceEntry> EDSequence {
  64. get {
  65. if (edSequence == null)
  66. edSequence = new List <EDSequenceEntry> ();
  67. return edSequence;
  68. }
  69. }
  70. #endif
  71. [Conditional ("DEBUG")]
  72. void InitDebugMode ()
  73. {
  74. #if DEBUG
  75. AppDomain.CurrentDomain.DomainUnload += new EventHandler (OnDomainUnload);
  76. #endif
  77. }
  78. [Conditional ("DEBUG")]
  79. void AddSequenceEntry (CacheItem item, EDSequenceEntryType type)
  80. {
  81. #if DEBUG
  82. EDSequence.Add (new EDSequenceEntry (CopyItem (item), type));
  83. #endif
  84. }
  85. #if DEBUG
  86. CacheItem CopyItem (CacheItem item)
  87. {
  88. CacheItem newItem;
  89. if (items.TryGetValue (item.Guid, out newItem))
  90. return newItem;
  91. newItem = new CacheItem ();
  92. newItem.Key = item.Key;
  93. newItem.AbsoluteExpiration = item.AbsoluteExpiration;
  94. newItem.SlidingExpiration = item.SlidingExpiration;
  95. newItem.Priority = item.Priority;
  96. newItem.LastChange = item.LastChange;
  97. newItem.ExpiresAt = item.ExpiresAt;
  98. newItem.Disabled = item.Disabled;
  99. newItem.Guid = item.Guid;
  100. items.Add (newItem.Guid, newItem);
  101. return newItem;
  102. }
  103. string CreateNewCacheItemInstanceCode (string indent, CacheItem item)
  104. {
  105. var sb = new StringBuilder (indent + "new CacheItem {");
  106. sb.AppendFormat ("Key = \"{0}\", ", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r"));
  107. sb.AppendFormat ("AbsoluteExpiration = DateTime.Parse (\"{0}\"), ", item.AbsoluteExpiration.ToString ());
  108. sb.AppendFormat ("SlidingExpiration = TimeSpan.Parse (\"{0}\"), ", item.SlidingExpiration.ToString ());
  109. sb.AppendFormat ("Priority = CacheItemPriority.{0}, ", item.Priority);
  110. sb.AppendFormat ("LastChange = DateTime.Parse (\"{0}\"), ", item.LastChange.ToString ());
  111. sb.AppendFormat ("ExpiresAt = {0}, ", item.ExpiresAt);
  112. sb.AppendFormat ("Disabled = {0}, ", item.Disabled.ToString ().ToLowerInvariant ());
  113. sb.AppendFormat ("Guid = new Guid (\"{0}\")}}, \n", item.Guid.ToString ());
  114. return sb.ToString ();
  115. }
  116. #endif
  117. [Conditional ("DEBUG")]
  118. public void OnItemDisable (CacheItem i)
  119. {
  120. #if DEBUG
  121. CacheItem item;
  122. if (!items.TryGetValue (i.Guid, out item))
  123. return;
  124. EDSequence.Add (new EDSequenceEntry (CopyItem (i), EDSequenceEntryType.Disable));
  125. #endif
  126. }
  127. #if DEBUG
  128. void OnDomainUnload (object sender, EventArgs e)
  129. {
  130. if (EDSequence.Count == 0) {
  131. Console.WriteLine ("No enqueue/dequeue sequence recorded.");
  132. return;
  133. }
  134. try {
  135. string filePath = Path.Combine (HttpRuntime.AppDomainAppPath, String.Format ("cache_pq_sequence_{0}.seq", DateTime.UtcNow.ToString ("yyyy-MM-dd_hh-mm-ss")));
  136. var settings = new XmlWriterSettings ();
  137. settings.Indent = true;
  138. settings.IndentChars = "\t";
  139. settings.NewLineChars = "\n";
  140. settings.Encoding = Encoding.UTF8;
  141. using (XmlWriter writer = XmlWriter.Create (filePath, settings)) {
  142. writer.WriteStartDocument (true);
  143. writer.WriteStartElement ("sequence");
  144. foreach (EDSequenceEntry entry in EDSequence) {
  145. writer.WriteStartElement ("entry");
  146. writer.WriteAttributeString ("type", entry.Type.ToString ());
  147. writer.WriteAttributeString ("key", entry.Item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r"));
  148. writer.WriteAttributeString ("absoluteExpiration", entry.Item.AbsoluteExpiration.Ticks.ToString ());
  149. writer.WriteAttributeString ("slidingExpiration", entry.Item.SlidingExpiration.Ticks.ToString ());
  150. writer.WriteAttributeString ("priority", entry.Item.Priority.ToString ());
  151. writer.WriteAttributeString ("lastChange", entry.Item.LastChange.Ticks.ToString ());
  152. writer.WriteAttributeString ("expiresAt", entry.Item.ExpiresAt.ToString ());
  153. writer.WriteAttributeString ("disabled", entry.Item.Disabled.ToString ());
  154. writer.WriteAttributeString ("guid", entry.Item.Guid.ToString ());
  155. writer.WriteEndElement ();
  156. }
  157. writer.WriteEndElement ();
  158. writer.Flush ();
  159. }
  160. } catch (Exception ex) {
  161. Console.WriteLine ("Failed to write enqueue/dequeue sequence file. Exception was caught:\n{0}",
  162. ex);
  163. }
  164. }
  165. #endif
  166. }
  167. }