2010-04-07 Marek Habersack <mhabersack@novell.com>
[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                 readonly Control owner;
48                 readonly HttpContext context;
49
50                 internal DataSourceCacheManager (int cacheDuration, string cacheKeyDependency,
51                         DataSourceCacheExpiry cacheExpirationPolicy, Control owner, HttpContext context)
52                 {
53                         this.cacheDuration = cacheDuration;
54                         this.cacheKeyDependency = cacheKeyDependency;
55                         this.cacheExpirationPolicy = cacheExpirationPolicy;
56                         this.controlID = owner.UniqueID;
57                         this.owner = owner;
58                         this.context = context;
59
60                         if (DataCache [controlID] == null)
61                                 DataCache [controlID] = new object ();
62                 }
63
64                 internal void Expire ()
65                 {
66                         DataCache [controlID] = new object ();
67                 }
68
69                 internal object GetCachedObject (string methodName, ParameterCollection parameters)
70                 {
71                         return DataCache [GetKeyFromParameters (methodName, parameters)];
72                 }
73
74                 internal void SetCachedObject (string methodName, ParameterCollection parameters, object o)
75                 {
76                         if (o == null)
77                                 return;
78
79                         string key = GetKeyFromParameters (methodName, parameters);
80
81                         if (DataCache [key] != null)
82                                 DataCache.Remove (key);
83
84                         DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
85                         TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
86
87                         if (cacheDuration > 0) {
88                                 if (cacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
89                                         absoluteExpiration = DateTime.Now.AddSeconds (cacheDuration);
90                                 else
91                                         slidindExpiraion = new TimeSpan (0, 0, cacheDuration);
92                         }
93
94                         string [] dependencies;
95                         if (cacheKeyDependency.Length > 0)
96                                 dependencies = new string [] { cacheKeyDependency };
97                         else
98                                 dependencies = new string [] { };
99
100                         DataCache.Add (key, o,
101                                 new CacheDependency (new string [] { }, dependencies),
102                                 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
103                 }
104
105                 static Cache DataCache
106                 {
107                         get {
108                                 if (HttpContext.Current != null)
109                                         return HttpContext.Current.InternalCache;
110
111                                 throw new InvalidOperationException ("HttpContext.Current is null.");
112                         }
113                 }
114
115                 string GetKeyFromParameters (string methodName, ParameterCollection parameters)
116                 {
117                         StringBuilder sb = new StringBuilder (methodName);
118
119                         if (owner != null)
120                                 sb.Append (owner.ID);
121
122                         for (int i = 0; i < parameters.Count; i++) {
123                                 sb.Append (parameters [i].Name);
124                                 sb.Append (parameters [i].GetValue (context, owner));
125                         }
126
127                         return sb.ToString ();
128                 }
129         }
130 }
131
132 #endif