BasePartialCachingControl.cs 5.3 KB

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