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;
61 protected BasePartialCachingControl()
65 internal string CtrlID {
66 get { return ctrl_id; }
67 set { ctrl_id = value; }
70 internal string Guid {
75 internal int Duration {
76 get { return duration; }
77 set { duration = value; }
80 internal string VaryByParams {
81 get { return varyby_params; }
82 set { varyby_params = value; }
85 internal string VaryByControls {
86 get { return varyby_controls; }
87 set { varyby_controls = value; }
90 internal string VaryByCustom {
91 get { return varyby_custom; }
92 set { varyby_custom = value; }
95 internal DateTime ExpirationTime {
96 get { return expirationTime; }
97 set { expirationTime = value; }
100 internal bool SlidingExpiration {
101 get { return slidingExpiration; }
102 set { slidingExpiration = value; }
105 internal string ProviderName {
109 internal abstract Control CreateControl ();
111 public override void Dispose ()
113 if (dependency != null) {
114 dependency.Dispose ();
119 void RetrieveCachedContents ()
121 cacheKey = CreateKey ();
123 OutputCacheProvider provider = GetProvider ();
124 cachedData = provider.Get (cacheKey) as string;
126 Cache cache = HttpRuntime.InternalCache;
127 cachedData = cache [cacheKey] as string;
131 OutputCacheProvider GetProvider ()
133 string providerName = ProviderName;
134 OutputCacheProvider provider;
136 if (String.IsNullOrEmpty (providerName))
137 provider = OutputCache.DefaultProvider;
139 provider = OutputCache.GetProvider (providerName);
140 if (provider == null)
141 provider = OutputCache.DefaultProvider;
147 void OnDependencyChanged (string key, object value, CacheItemRemovedReason reason)
149 Console.WriteLine ("{0}.OnDependencyChanged (\"{0}\", {1}, {2})", this, key, value, reason);
150 GetProvider ().Remove (key);
153 internal override void InitRecursive (Control namingContainer)
155 RetrieveCachedContents ();
156 if (cachedData == null) {
157 control = CreateControl ();
158 Controls.Add (control);
162 base.InitRecursive (namingContainer);
165 protected internal override void OnInit (EventArgs e)
167 control = CreateControl ();
168 Controls.Add (control);
171 protected internal override void Render (HtmlTextWriter output)
174 RetrieveCachedContents ();
176 if (cachedData != null) {
177 output.Write (cachedData);
181 if (control == null) {
182 base.Render (output);
186 HttpContext context = HttpContext.Current;
187 StringWriter writer = new StringWriter ();
188 TextWriter prev = context.Response.SetTextWriter (writer);
189 HtmlTextWriter txt_writer = new HtmlTextWriter (writer);
192 control.RenderControl (txt_writer);
194 text = writer.ToString ();
195 context.Response.SetTextWriter (prev);
199 OutputCacheProvider provider = GetProvider ();
200 DateTime utcExpire = DateTime.UtcNow.AddSeconds (duration);
201 provider.Set (cacheKey, text, utcExpire);;
202 context.InternalCache.Insert (cacheKey, text, dependency, utcExpire.ToLocalTime (),
203 Cache.NoSlidingExpiration, CacheItemPriority.Normal,
206 context.InternalCache.Insert (cacheKey, text, dependency,
207 DateTime.Now.AddSeconds (duration),
208 Cache.NoSlidingExpiration,
209 CacheItemPriority.Normal, null);
213 public ControlCachePolicy CachePolicy
216 if (cachePolicy == null)
217 cachePolicy = new ControlCachePolicy (this);
223 public CacheDependency Dependency {
224 get {return dependency;}
225 set {dependency = value;}
230 StringBuilder builder = new StringBuilder ();
231 HttpContext context = HttpContext.Current;
233 builder.Append ("PartialCachingControl\n");
234 builder.Append ("GUID: " + guid + "\n");
236 if (varyby_params != null && varyby_params.Length > 0) {
237 string[] prms = varyby_params.Split (';');
238 for (int i=0; i<prms.Length; i++) {
239 string val = context.Request.Params [prms [i]];
240 builder.Append ("VP:");
241 builder.Append (prms [i]);
242 builder.Append ('=');
243 builder.Append (val != null ? val : "__null__");
244 builder.Append ('\n');
248 if (varyby_controls != null && varyby_params.Length > 0) {
249 string[] prms = varyby_controls.Split (';');
250 for (int i=0; i<prms.Length; i++) {
251 string val = context.Request.Params [prms [i]];
252 builder.Append ("VCN:");
253 builder.Append (prms [i]);
254 builder.Append ('=');
255 builder.Append (val != null ? val : "__null__");
256 builder.Append ('\n');
260 if (varyby_custom != null) {
261 string val = context.ApplicationInstance.GetVaryByCustomString (context,
263 builder.Append ("VC:");
264 builder.Append (varyby_custom);
265 builder.Append ('=');
266 builder.Append (val != null ? val : "__null__");
267 builder.Append ('\n');
270 return builder.ToString ();