2
0

BasePartialCachingControl.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Web.UI.BasePartialCachingControl.cs
  3. //
  4. // Author:
  5. // Andreas Nahr ([email protected])
  6. // Jackson Harper ([email protected])
  7. //
  8. // (C) 2003 Andreas Nahr
  9. // (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.IO;
  33. using System.Text;
  34. using System.ComponentModel;
  35. using System.Web.Caching;
  36. namespace System.Web.UI
  37. {
  38. [ToolboxItem (false)]
  39. public abstract class BasePartialCachingControl : Control
  40. {
  41. private CacheDependency dependency;
  42. private string ctrl_id;
  43. private string guid;
  44. private int duration;
  45. private string varyby_params;
  46. private string varyby_controls;
  47. private string varyby_custom;
  48. private Control control;
  49. protected BasePartialCachingControl()
  50. {
  51. }
  52. internal string CtrlID {
  53. get { return ctrl_id; }
  54. set { ctrl_id = value; }
  55. }
  56. internal string Guid {
  57. get { return guid; }
  58. set { guid = value; }
  59. }
  60. internal int Duration {
  61. get { return duration; }
  62. set { duration = value; }
  63. }
  64. internal string VaryByParams {
  65. get { return varyby_params; }
  66. set { varyby_params = value; }
  67. }
  68. internal string VaryByControls {
  69. get { return varyby_controls; }
  70. set { varyby_controls = value; }
  71. }
  72. internal string VaryByCustom {
  73. get { return varyby_custom; }
  74. set { varyby_custom = value; }
  75. }
  76. internal abstract Control CreateControl ();
  77. public override void Dispose ()
  78. {
  79. if (dependency != null) {
  80. dependency.Dispose ();
  81. dependency = null;
  82. }
  83. }
  84. protected override void OnInit (EventArgs e)
  85. {
  86. control = CreateControl ();
  87. Controls.Add (control);
  88. }
  89. protected override void Render (HtmlTextWriter output)
  90. {
  91. Cache cache = HttpRuntime.Cache;
  92. string key = CreateKey ();
  93. string data = cache [key] as string;
  94. if (data != null) {
  95. output.Write (data);
  96. return;
  97. }
  98. HttpContext context = HttpContext.Current;
  99. StringWriter writer = new StringWriter ();
  100. TextWriter prev = context.Response.SetTextWriter (writer);
  101. HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
  102. string text;
  103. try {
  104. control.RenderControl (txt_writer);
  105. } finally {
  106. text = writer.ToString ();
  107. context.Response.SetTextWriter (prev);
  108. output.Write (text);
  109. }
  110. context.Cache.InsertPrivate (key, text, dependency,
  111. DateTime.Now.AddSeconds (duration),
  112. Cache.NoSlidingExpiration,
  113. CacheItemPriority.Normal, null);
  114. }
  115. public CacheDependency Dependency {
  116. get {return dependency;}
  117. set {dependency = value;}
  118. }
  119. private string CreateKey ()
  120. {
  121. StringBuilder builder = new StringBuilder ();
  122. HttpContext context = HttpContext.Current;
  123. builder.Append ("PartialCachingControl\n");
  124. builder.Append ("GUID: " + guid + "\n");
  125. if (varyby_params != null && varyby_params.Length > 0) {
  126. string[] prms = varyby_params.Split (';');
  127. for (int i=0; i<prms.Length; i++) {
  128. string val = context.Request.Params [prms [i]];
  129. builder.Append ("VP:");
  130. builder.Append (prms [i]);
  131. builder.Append ('=');
  132. builder.Append (val != null ? val : "__null__");
  133. builder.Append ('\n');
  134. }
  135. }
  136. if (varyby_controls != null && varyby_params.Length > 0) {
  137. string[] prms = varyby_controls.Split (';');
  138. for (int i=0; i<prms.Length; i++) {
  139. string val = context.Request.Params [prms [i]];
  140. builder.Append ("VCN:");
  141. builder.Append (prms [i]);
  142. builder.Append ('=');
  143. builder.Append (val != null ? val : "__null__");
  144. builder.Append ('\n');
  145. }
  146. }
  147. if (varyby_custom != null) {
  148. string val = context.ApplicationInstance.GetVaryByCustomString (context,
  149. varyby_custom);
  150. builder.Append ("VC:");
  151. builder.Append (varyby_custom);
  152. builder.Append ('=');
  153. builder.Append (val != null ? val : "__null__");
  154. builder.Append ('\n');
  155. }
  156. return builder.ToString ();
  157. }
  158. }
  159. }