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 CacheDependency dependency;
51 string varyby_controls;
53 DateTime expirationTime;
54 bool slidingExpiration;
57 ControlCachePolicy cachePolicy;
59 protected BasePartialCachingControl()
63 internal string CtrlID {
64 get { return ctrl_id; }
65 set { ctrl_id = value; }
68 internal string Guid {
73 internal int Duration {
74 get { return duration; }
75 set { duration = value; }
78 internal string VaryByParams {
79 get { return varyby_params; }
80 set { varyby_params = value; }
83 internal string VaryByControls {
84 get { return varyby_controls; }
85 set { varyby_controls = value; }
88 internal string VaryByCustom {
89 get { return varyby_custom; }
90 set { varyby_custom = value; }
93 internal DateTime ExpirationTime {
94 get { return expirationTime; }
95 set { expirationTime = value; }
98 internal bool SlidingExpiration {
99 get { return slidingExpiration; }
100 set { slidingExpiration = value; }
103 internal abstract Control CreateControl ();
105 public override void Dispose ()
107 if (dependency != null) {
108 dependency.Dispose ();
113 protected internal override void OnInit (EventArgs e)
115 control = CreateControl ();
116 Controls.Add (control);
119 protected internal override void Render (HtmlTextWriter output)
121 Cache cache = HttpRuntime.InternalCache;
122 string key = CreateKey ();
123 string data = cache [key] as string;
130 HttpContext context = HttpContext.Current;
131 StringWriter writer = new StringWriter ();
132 TextWriter prev = context.Response.SetTextWriter (writer);
133 HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
136 control.RenderControl (txt_writer);
138 text = writer.ToString ();
139 context.Response.SetTextWriter (prev);
143 context.InternalCache.Insert (key, text, dependency,
144 DateTime.Now.AddSeconds (duration),
145 Cache.NoSlidingExpiration,
146 CacheItemPriority.Normal, null);
149 public ControlCachePolicy CachePolicy
152 if (cachePolicy == null)
153 cachePolicy = new ControlCachePolicy (this);
159 public CacheDependency Dependency {
160 get {return dependency;}
161 set {dependency = value;}
166 StringBuilder builder = new StringBuilder ();
167 HttpContext context = HttpContext.Current;
169 builder.Append ("PartialCachingControl\n");
170 builder.Append ("GUID: " + guid + "\n");
172 if (varyby_params != null && varyby_params.Length > 0) {
173 string[] prms = varyby_params.Split (';');
174 for (int i=0; i<prms.Length; i++) {
175 string val = context.Request.Params [prms [i]];
176 builder.Append ("VP:");
177 builder.Append (prms [i]);
178 builder.Append ('=');
179 builder.Append (val != null ? val : "__null__");
180 builder.Append ('\n');
184 if (varyby_controls != null && varyby_params.Length > 0) {
185 string[] prms = varyby_controls.Split (';');
186 for (int i=0; i<prms.Length; i++) {
187 string val = context.Request.Params [prms [i]];
188 builder.Append ("VCN:");
189 builder.Append (prms [i]);
190 builder.Append ('=');
191 builder.Append (val != null ? val : "__null__");
192 builder.Append ('\n');
196 if (varyby_custom != null) {
197 string val = context.ApplicationInstance.GetVaryByCustomString (context,
199 builder.Append ("VC:");
200 builder.Append (varyby_custom);
201 builder.Append ('=');
202 builder.Append (val != null ? val : "__null__");
203 builder.Append ('\n');
206 return builder.ToString ();