BasePartialCachingControl.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #if NET_2_0
  85. protected internal
  86. #else
  87. protected
  88. #endif
  89. override void OnInit (EventArgs e)
  90. {
  91. control = CreateControl ();
  92. Controls.Add (control);
  93. }
  94. #if NET_2_0
  95. protected internal
  96. #else
  97. protected
  98. #endif
  99. override void Render (HtmlTextWriter output)
  100. {
  101. Cache cache = HttpRuntime.Cache;
  102. string key = CreateKey ();
  103. string data = cache [key] as string;
  104. if (data != null) {
  105. output.Write (data);
  106. return;
  107. }
  108. HttpContext context = HttpContext.Current;
  109. StringWriter writer = new StringWriter ();
  110. TextWriter prev = context.Response.SetTextWriter (writer);
  111. HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
  112. string text;
  113. try {
  114. control.RenderControl (txt_writer);
  115. } finally {
  116. text = writer.ToString ();
  117. context.Response.SetTextWriter (prev);
  118. output.Write (text);
  119. }
  120. context.Cache.InsertPrivate (key, text, dependency,
  121. DateTime.Now.AddSeconds (duration),
  122. Cache.NoSlidingExpiration,
  123. CacheItemPriority.Normal, null);
  124. }
  125. #if NET_2_0
  126. public ControlCachePolicy CachePolicy
  127. {
  128. get {
  129. throw new NotImplementedException ();
  130. }
  131. }
  132. #endif
  133. public CacheDependency Dependency {
  134. get {return dependency;}
  135. set {dependency = value;}
  136. }
  137. private string CreateKey ()
  138. {
  139. StringBuilder builder = new StringBuilder ();
  140. HttpContext context = HttpContext.Current;
  141. builder.Append ("PartialCachingControl\n");
  142. builder.Append ("GUID: " + guid + "\n");
  143. if (varyby_params != null && varyby_params.Length > 0) {
  144. string[] prms = varyby_params.Split (';');
  145. for (int i=0; i<prms.Length; i++) {
  146. string val = context.Request.Params [prms [i]];
  147. builder.Append ("VP:");
  148. builder.Append (prms [i]);
  149. builder.Append ('=');
  150. builder.Append (val != null ? val : "__null__");
  151. builder.Append ('\n');
  152. }
  153. }
  154. if (varyby_controls != null && varyby_params.Length > 0) {
  155. string[] prms = varyby_controls.Split (';');
  156. for (int i=0; i<prms.Length; i++) {
  157. string val = context.Request.Params [prms [i]];
  158. builder.Append ("VCN:");
  159. builder.Append (prms [i]);
  160. builder.Append ('=');
  161. builder.Append (val != null ? val : "__null__");
  162. builder.Append ('\n');
  163. }
  164. }
  165. if (varyby_custom != null) {
  166. string val = context.ApplicationInstance.GetVaryByCustomString (context,
  167. varyby_custom);
  168. builder.Append ("VC:");
  169. builder.Append (varyby_custom);
  170. builder.Append ('=');
  171. builder.Append (val != null ? val : "__null__");
  172. builder.Append ('\n');
  173. }
  174. return builder.ToString ();
  175. }
  176. }
  177. }