[asp.net] Removed the DEBUG ifdefs, for the tests to compile.
[mono.git] / mcs / class / System.Web / System.Web.UI / BasePartialCachingControl.cs
index 3debb830b332898ff6073c03616e521d470469b7..4480d49092c103169c33048821662de17ef838f1 100644 (file)
@@ -43,15 +43,20 @@ namespace System.Web.UI
        [ToolboxItem (false)]
        public abstract class BasePartialCachingControl : Control
        {
-               private CacheDependency dependency;
-               private string ctrl_id;
-               private string guid;
-               private int duration;
-               private string varyby_params;
-               private string varyby_controls;
-               private string varyby_custom;
-
-               private Control control;
+               CacheDependency dependency;
+               string ctrl_id;
+               string guid;
+               int duration;
+               string varyby_params;
+               string varyby_controls;
+               string varyby_custom;
+               DateTime expirationTime;
+               bool slidingExpiration;
+               
+               Control control;
+               ControlCachePolicy cachePolicy;
+               string cacheKey;
+               string cachedData;
                
                protected BasePartialCachingControl()
                {
@@ -87,6 +92,20 @@ namespace System.Web.UI
                        set { varyby_custom = value; }
                }
 
+               internal DateTime ExpirationTime {
+                       get { return expirationTime; }
+                       set { expirationTime = value; }
+               }
+
+               internal bool SlidingExpiration {
+                       get { return slidingExpiration; }
+                       set { slidingExpiration = value; }
+               }
+#if NET_4_0
+               internal string ProviderName {
+                       get; set;
+               }
+#endif
                internal abstract Control CreateControl ();
 
                public override void Dispose ()
@@ -97,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.Cache;
-                       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);
@@ -136,28 +195,37 @@ namespace System.Web.UI
                                context.Response.SetTextWriter (prev);
                                output.Write (text);
                        }
-
-                       context.Cache.InsertPrivate (key, text, dependency,
-                                               DateTime.Now.AddSeconds (duration),
-                                               Cache.NoSlidingExpiration,
-                                               CacheItemPriority.Normal, null);
+#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 {
-                               throw new NotImplementedException ();
+                               if (cachePolicy == null)
+                                       cachePolicy = new ControlCachePolicy (this);
+
+                               return cachePolicy;
                        }
                }
-#endif
 
                public CacheDependency Dependency {
                        get {return dependency;}
                        set {dependency = value;}
                }
 
-               private string CreateKey ()
+               string CreateKey ()
                {
                        StringBuilder builder = new StringBuilder ();
                        HttpContext context = HttpContext.Current;