New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataSourceCacheManager.cs
1 //
2 // System.Web.UI.WebControls.DataSourceCacheManager
3 //
4 // Authors:
5 //      Vladimir Krasnov (vladimirk@mainsoft.com)
6 //
7 // Mainsoft (www.mainsoft.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Web;
33 using System.Web.UI;
34 using System.Web.Caching;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Text;
38
39 namespace System.Web.UI.WebControls
40 {
41         internal class DataSourceCacheManager
42         {
43                 readonly int cacheDuration;
44                 readonly string cacheKeyDependency;
45                 readonly string controlID;
46                 readonly DataSourceCacheExpiry cacheExpirationPolicy;
47
48                 internal DataSourceCacheManager (int cacheDuration, string cacheKeyDependency,
49                         DataSourceCacheExpiry cacheExpirationPolicy, string controlID)
50                 {
51                         this.cacheDuration = cacheDuration;
52                         this.cacheKeyDependency = cacheKeyDependency;
53                         this.cacheExpirationPolicy = cacheExpirationPolicy;
54                         this.controlID = controlID;
55
56                         if (DataCache [controlID] == null)
57                                 DataCache [controlID] = new object ();
58                 }
59
60                 internal void Expire ()
61                 {
62                         DataCache [controlID] = new object ();
63                 }
64
65                 internal object GetCachedObject (string methodName, ParameterCollection parameters)
66                 {
67                         return DataCache [GetKeyFromParameters (methodName, parameters)];
68                 }
69
70                 internal void SetCachedObject (string methodName, ParameterCollection parameters, object o)
71                 {
72                         if (o == null)
73                                 return;
74
75                         string key = GetKeyFromParameters (methodName, parameters);
76
77                         if (DataCache [key] != null)
78                                 DataCache.Remove (key);
79
80                         DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
81                         TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
82
83                         if (cacheDuration > 0) {
84                                 if (cacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
85                                         absoluteExpiration = DateTime.Now.AddSeconds (cacheDuration);
86                                 else
87                                         slidindExpiraion = new TimeSpan (cacheDuration * 10000);
88                         }
89
90                         string [] dependencies;
91                         if (cacheKeyDependency.Length > 0)
92                                 dependencies = new string [] { cacheKeyDependency, controlID };
93                         else
94                                 dependencies = new string [] { controlID };
95
96                         DataCache.Add (key, o,
97                                 new CacheDependency (new string [] { }, dependencies),
98                                 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
99                 }
100
101                 static Cache DataCache
102                 {
103                         get
104                         {
105                                 if (HttpContext.Current != null)
106                                         return HttpContext.Current.Cache;
107
108                                 throw new InvalidOperationException ("HttpContext.Current is null.");
109                         }
110                 }
111
112                 static string GetKeyFromParameters (string methodName, ParameterCollection parameters)
113                 {
114                         StringBuilder sb = new StringBuilder (methodName);
115
116                         for (int i = 0; i < parameters.Count; i++) {
117                                 sb.Append (parameters [i].Name);
118                                 sb.Append (parameters [i].DefaultValue);
119                         }
120
121                         return sb.ToString ();
122                 }
123         }
124 }
125
126 #endif