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;
47 readonly Control owner;
48 readonly HttpContext context;
50 internal DataSourceCacheManager (int cacheDuration, string cacheKeyDependency,
51 DataSourceCacheExpiry cacheExpirationPolicy, Control owner, HttpContext context)
53 this.cacheDuration = cacheDuration;
54 this.cacheKeyDependency = cacheKeyDependency;
55 this.cacheExpirationPolicy = cacheExpirationPolicy;
56 this.controlID = owner.UniqueID;
58 this.context = context;
60 if (DataCache [controlID] == null)
61 DataCache [controlID] = new object ();
64 internal void Expire ()
66 DataCache [controlID] = new object ();
69 internal object GetCachedObject (string methodName, ParameterCollection parameters)
71 return DataCache [GetKeyFromParameters (methodName, parameters)];
74 internal void SetCachedObject (string methodName, ParameterCollection parameters, object o)
79 string key = GetKeyFromParameters (methodName, parameters);
81 if (DataCache [key] != null)
82 DataCache.Remove (key);
84 DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
85 TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
87 if (cacheDuration > 0) {
88 if (cacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
89 absoluteExpiration = DateTime.Now.AddSeconds (cacheDuration);
91 slidindExpiraion = new TimeSpan (0, 0, cacheDuration);
94 string [] dependencies;
95 if (cacheKeyDependency.Length > 0)
96 dependencies = new string [] { cacheKeyDependency };
98 dependencies = new string [] { };
100 DataCache.Add (key, o,
101 new CacheDependency (new string [] { }, dependencies),
102 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
105 static Cache DataCache
108 if (HttpContext.Current != null)
109 return HttpContext.Current.InternalCache;
111 throw new InvalidOperationException ("HttpContext.Current is null.");
115 string GetKeyFromParameters (string methodName, ParameterCollection parameters)
117 StringBuilder sb = new StringBuilder (methodName);
120 sb.Append (owner.ID);
122 for (int i = 0; i < parameters.Count; i++) {
123 sb.Append (parameters [i].Name);
124 sb.Append (parameters [i].GetValue (context, owner));
127 return sb.ToString ();