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 // (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.ComponentModel;
37 using System.Web.Caching;
39 namespace System.Web.UI
42 public abstract class BasePartialCachingControl : Control
44 private CacheDependency dependency;
45 private string ctrl_id;
48 private string varyby_params;
49 private string varyby_controls;
50 private string varyby_custom;
52 private Control control;
54 protected BasePartialCachingControl()
58 internal string CtrlID {
59 get { return ctrl_id; }
60 set { ctrl_id = value; }
63 internal string Guid {
68 internal int Duration {
69 get { return duration; }
70 set { duration = value; }
73 internal string VaryByParams {
74 get { return varyby_params; }
75 set { varyby_params = value; }
78 internal string VaryByControls {
79 get { return varyby_controls; }
80 set { varyby_controls = value; }
83 internal string VaryByCustom {
84 get { return varyby_custom; }
85 set { varyby_custom = value; }
88 internal abstract Control CreateControl ();
90 public override void Dispose ()
92 if (dependency != null) {
93 dependency.Dispose ();
103 override void OnInit (EventArgs e)
105 control = CreateControl ();
106 Controls.Add (control);
114 override void Render (HtmlTextWriter output)
116 Cache cache = HttpRuntime.Cache;
117 string key = CreateKey ();
118 string data = cache [key] as string;
125 HttpContext context = HttpContext.Current;
126 StringWriter writer = new StringWriter ();
127 TextWriter prev = context.Response.SetTextWriter (writer);
128 HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
131 control.RenderControl (txt_writer);
133 text = writer.ToString ();
134 context.Response.SetTextWriter (prev);
138 context.Cache.InsertPrivate (key, text, dependency,
139 DateTime.Now.AddSeconds (duration),
140 Cache.NoSlidingExpiration,
141 CacheItemPriority.Normal, null);
145 public ControlCachePolicy CachePolicy
148 throw new NotImplementedException ();
153 public CacheDependency Dependency {
154 get {return dependency;}
155 set {dependency = value;}
158 private string CreateKey ()
160 StringBuilder builder = new StringBuilder ();
161 HttpContext context = HttpContext.Current;
163 builder.Append ("PartialCachingControl\n");
164 builder.Append ("GUID: " + guid + "\n");
166 if (varyby_params != null && varyby_params.Length > 0) {
167 string[] prms = varyby_params.Split (';');
168 for (int i=0; i<prms.Length; i++) {
169 string val = context.Request.Params [prms [i]];
170 builder.Append ("VP:");
171 builder.Append (prms [i]);
172 builder.Append ('=');
173 builder.Append (val != null ? val : "__null__");
174 builder.Append ('\n');
178 if (varyby_controls != null && varyby_params.Length > 0) {
179 string[] prms = varyby_controls.Split (';');
180 for (int i=0; i<prms.Length; i++) {
181 string val = context.Request.Params [prms [i]];
182 builder.Append ("VCN:");
183 builder.Append (prms [i]);
184 builder.Append ('=');
185 builder.Append (val != null ? val : "__null__");
186 builder.Append ('\n');
190 if (varyby_custom != null) {
191 string val = context.ApplicationInstance.GetVaryByCustomString (context,
193 builder.Append ("VC:");
194 builder.Append (varyby_custom);
195 builder.Append ('=');
196 builder.Append (val != null ? val : "__null__");
197 builder.Append ('\n');
200 return builder.ToString ();