[asp.net] Implemented the 4.0 type, FileLevelMasterPageControlBuilder
[mono.git] / mcs / class / System.Web / System.Web.UI / BasePartialCachingControl.cs
index 71002f76b92e405b5e46fa39efbfc162b1d9d5f7..4480d49092c103169c33048821662de17ef838f1 100644 (file)
@@ -54,9 +54,9 @@ namespace System.Web.UI
                bool slidingExpiration;
                
                Control control;
-#if NET_2_0
                ControlCachePolicy cachePolicy;
-#endif
+               string cacheKey;
+               string cachedData;
                
                protected BasePartialCachingControl()
                {
@@ -101,7 +101,11 @@ namespace System.Web.UI
                        get { return slidingExpiration; }
                        set { slidingExpiration = value; }
                }
-               
+#if NET_4_0
+               internal string ProviderName {
+                       get; set;
+               }
+#endif
                internal abstract Control CreateControl ();
 
                public override void Dispose ()
@@ -112,33 +116,73 @@ namespace System.Web.UI
                        }
                }
 
-#if NET_2_0
-               protected internal
+               void RetrieveCachedContents ()
+               {
+                       cacheKey = CreateKey ();
+#if NET_4_0
+                       OutputCacheProvider provider = GetProvider ();
+                       cachedData = provider.Get (cacheKey) as string;
 #else
-               protected
+                       Cache cache = HttpRuntime.InternalCache;
+                       cachedData = cache [cacheKey] as string;
 #endif
-               override void OnInit (EventArgs e)
+               }
+#if NET_4_0
+               OutputCacheProvider GetProvider ()
+               {
+                       string providerName = ProviderName;
+                       OutputCacheProvider provider;
+
+                       if (String.IsNullOrEmpty (providerName))
+                               provider = OutputCache.DefaultProvider;
+                       else {
+                               provider = OutputCache.GetProvider (providerName);
+                               if (provider == null)
+                                       provider = OutputCache.DefaultProvider;
+                       }
+
+                       return provider;
+               }
+               
+               void OnDependencyChanged (string key, object value, CacheItemRemovedReason reason)
+               {
+                       Console.WriteLine ("{0}.OnDependencyChanged (\"{0}\", {1}, {2})", this, key, value, reason);
+                       GetProvider ().Remove (key);
+               }
+               
+               internal override void InitRecursive (Control namingContainer)
+               {
+                       RetrieveCachedContents ();
+                       if (cachedData == null) {
+                               control = CreateControl ();
+                               Controls.Add (control);
+                       } else
+                               control = null;
+                       
+                       base.InitRecursive (namingContainer);
+               }
+#else
+               protected internal override void OnInit (EventArgs e)
                {
                        control = CreateControl ();
                        Controls.Add (control);
                }
-
-#if NET_2_0
-               protected internal
-#else
-               protected
 #endif
-               override void Render (HtmlTextWriter output)
+               protected internal override void Render (HtmlTextWriter output)
                {
-                       Cache cache = HttpRuntime.InternalCache;
-                       string key = CreateKey ();
-                       string data = cache [key] as string;
-
-                       if (data != null) {
-                               output.Write (data);
+#if !NET_4_0
+                       RetrieveCachedContents ();
+#endif
+                       if (cachedData != null) {
+                               output.Write (cachedData);
                                return;
                        }
 
+                       if (control == null) {
+                               base.Render (output);
+                               return;
+                       }
+                       
                        HttpContext context = HttpContext.Current;
                        StringWriter writer = new StringWriter ();
                        TextWriter prev = context.Response.SetTextWriter (writer);
@@ -151,14 +195,21 @@ namespace System.Web.UI
                                context.Response.SetTextWriter (prev);
                                output.Write (text);
                        }
-
-                       context.InternalCache.Insert (key, text, dependency,
+#if NET_4_0
+                       OutputCacheProvider provider = GetProvider ();
+                       DateTime utcExpire = DateTime.UtcNow.AddSeconds (duration);
+                       provider.Set (cacheKey, text, utcExpire);;
+                       context.InternalCache.Insert (cacheKey, text, dependency, utcExpire.ToLocalTime (),
+                                                     Cache.NoSlidingExpiration, CacheItemPriority.Normal,
+                                                     null);
+#else
+                       context.InternalCache.Insert (cacheKey, text, dependency,
                                                      DateTime.Now.AddSeconds (duration),
                                                      Cache.NoSlidingExpiration,
                                                      CacheItemPriority.Normal, null);
+#endif
                }
 
-#if NET_2_0
                public ControlCachePolicy CachePolicy 
                {
                        get {
@@ -168,7 +219,6 @@ namespace System.Web.UI
                                return cachePolicy;
                        }
                }
-#endif
 
                public CacheDependency Dependency {
                        get {return dependency;}