2 // System.Web.UI.WebControls.DataSourceCacheManager
5 // Vladimir Krasnov (vladimirk@mainsoft.com)
7 // Mainsoft (www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
34 using System.Web.Caching;
35 using System.Collections;
36 using System.Collections.Specialized;
39 namespace System.Web.UI.WebControls
41 internal class DataSourceCacheManager
43 readonly int cacheDuration;
44 readonly string cacheKeyDependency;
45 readonly string controlID;
46 readonly DataSourceCacheExpiry cacheExpirationPolicy;
48 internal DataSourceCacheManager (int cacheDuration, string cacheKeyDependency,
49 DataSourceCacheExpiry cacheExpirationPolicy, string controlID)
51 this.cacheDuration = cacheDuration;
52 this.cacheKeyDependency = cacheKeyDependency;
53 this.cacheExpirationPolicy = cacheExpirationPolicy;
54 this.controlID = controlID;
56 if (DataCache [controlID] == null)
57 DataCache [controlID] = new object ();
60 internal void Expire ()
62 DataCache [controlID] = new object ();
65 internal object GetCachedObject (string methodName, ParameterCollection parameters)
67 return DataCache [GetKeyFromParameters (methodName, parameters)];
70 internal void SetCachedObject (string methodName, ParameterCollection parameters, object o)
75 string key = GetKeyFromParameters (methodName, parameters);
77 if (DataCache [key] != null)
78 DataCache.Remove (key);
80 DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
81 TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
83 if (cacheDuration > 0) {
84 if (cacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
85 absoluteExpiration = DateTime.Now.AddSeconds (cacheDuration);
87 slidindExpiraion = new TimeSpan (cacheDuration * 10000);
90 string [] dependencies;
91 if (cacheKeyDependency.Length > 0)
92 dependencies = new string [] { cacheKeyDependency, controlID };
94 dependencies = new string [] { controlID };
96 DataCache.Add (key, o,
97 new CacheDependency (new string [] { }, dependencies),
98 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
101 static Cache DataCache
105 if (HttpContext.Current != null)
106 return HttpContext.Current.Cache;
108 throw new InvalidOperationException ("HttpContext.Current is null.");
112 static string GetKeyFromParameters (string methodName, ParameterCollection parameters)
114 StringBuilder sb = new StringBuilder (methodName);
116 for (int i = 0; i < parameters.Count; i++) {
117 sb.Append (parameters [i].Name);
118 sb.Append (parameters [i].DefaultValue);
121 return sb.ToString ();