2 // System.Web.UI.BasePartialCachingControl.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // Jackson Harper (jackson@ximian.com)
8 // (C) 2003 Andreas Nahr
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.ComponentModel;
34 using System.Security.Permissions;
35 using System.Web.Caching;
37 namespace System.Web.UI
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 public abstract class BasePartialCachingControl : Control
46 private CacheDependency dependency;
47 private string ctrl_id;
50 private string varyby_params;
51 private string varyby_controls;
52 private string varyby_custom;
54 private Control control;
56 protected BasePartialCachingControl()
60 internal string CtrlID {
61 get { return ctrl_id; }
62 set { ctrl_id = value; }
65 internal string Guid {
70 internal int Duration {
71 get { return duration; }
72 set { duration = value; }
75 internal string VaryByParams {
76 get { return varyby_params; }
77 set { varyby_params = value; }
80 internal string VaryByControls {
81 get { return varyby_controls; }
82 set { varyby_controls = value; }
85 internal string VaryByCustom {
86 get { return varyby_custom; }
87 set { varyby_custom = value; }
90 internal abstract Control CreateControl ();
92 public override void Dispose ()
94 if (dependency != null) {
95 dependency.Dispose ();
105 override void OnInit (EventArgs e)
107 control = CreateControl ();
108 Controls.Add (control);
116 override void Render (HtmlTextWriter output)
118 Cache cache = HttpRuntime.InternalCache;
119 string key = CreateKey ();
120 string data = cache [key] as string;
127 HttpContext context = HttpContext.Current;
128 StringWriter writer = new StringWriter ();
129 TextWriter prev = context.Response.SetTextWriter (writer);
130 HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
133 control.RenderControl (txt_writer);
135 text = writer.ToString ();
136 context.Response.SetTextWriter (prev);
140 context.InternalCache.Insert (key, text, dependency,
141 DateTime.Now.AddSeconds (duration),
142 Cache.NoSlidingExpiration,
143 CacheItemPriority.Normal, null);
147 public ControlCachePolicy CachePolicy
150 throw new NotImplementedException ();
155 public CacheDependency Dependency {
156 get {return dependency;}
157 set {dependency = value;}
160 private string CreateKey ()
162 StringBuilder builder = new StringBuilder ();
163 HttpContext context = HttpContext.Current;
165 builder.Append ("PartialCachingControl\n");
166 builder.Append ("GUID: " + guid + "\n");
168 if (varyby_params != null && varyby_params.Length > 0) {
169 string[] prms = varyby_params.Split (';');
170 for (int i=0; i<prms.Length; i++) {
171 string val = context.Request.Params [prms [i]];
172 builder.Append ("VP:");
173 builder.Append (prms [i]);
174 builder.Append ('=');
175 builder.Append (val != null ? val : "__null__");
176 builder.Append ('\n');
180 if (varyby_controls != null && varyby_params.Length > 0) {
181 string[] prms = varyby_controls.Split (';');
182 for (int i=0; i<prms.Length; i++) {
183 string val = context.Request.Params [prms [i]];
184 builder.Append ("VCN:");
185 builder.Append (prms [i]);
186 builder.Append ('=');
187 builder.Append (val != null ? val : "__null__");
188 builder.Append ('\n');
192 if (varyby_custom != null) {
193 string val = context.ApplicationInstance.GetVaryByCustomString (context,
195 builder.Append ("VC:");
196 builder.Append (varyby_custom);
197 builder.Append ('=');
198 builder.Append (val != null ? val : "__null__");
199 builder.Append ('\n');
202 return builder.ToString ();