DataSourceCacheManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // System.Web.UI.WebControls.DataSourceCacheManager
  3. //
  4. // Authors:
  5. // Vladimir Krasnov ([email protected])
  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. #if NET_2_0
  29. using System;
  30. using System.Web;
  31. using System.Web.UI;
  32. using System.Web.Caching;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.Text;
  36. namespace System.Web.UI.WebControls
  37. {
  38. internal class DataSourceCacheManager
  39. {
  40. readonly int cacheDuration;
  41. readonly string cacheKeyDependency;
  42. readonly string controlID;
  43. readonly DataSourceCacheExpiry cacheExpirationPolicy;
  44. readonly Control owner;
  45. readonly HttpContext context;
  46. internal DataSourceCacheManager (int cacheDuration, string cacheKeyDependency,
  47. DataSourceCacheExpiry cacheExpirationPolicy, Control owner, HttpContext context)
  48. {
  49. this.cacheDuration = cacheDuration;
  50. this.cacheKeyDependency = cacheKeyDependency;
  51. this.cacheExpirationPolicy = cacheExpirationPolicy;
  52. this.controlID = owner.UniqueID;
  53. this.owner = owner;
  54. this.context = context;
  55. if (DataCache [controlID] == null)
  56. DataCache [controlID] = new object ();
  57. }
  58. internal void Expire ()
  59. {
  60. DataCache [controlID] = new object ();
  61. }
  62. internal object GetCachedObject (string methodName, ParameterCollection parameters)
  63. {
  64. return DataCache [GetKeyFromParameters (methodName, parameters)];
  65. }
  66. internal void SetCachedObject (string methodName, ParameterCollection parameters, object o)
  67. {
  68. if (o == null)
  69. return;
  70. string key = GetKeyFromParameters (methodName, parameters);
  71. if (DataCache [key] != null)
  72. DataCache.Remove (key);
  73. DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
  74. TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
  75. if (cacheDuration > 0) {
  76. if (cacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
  77. absoluteExpiration = DateTime.Now.AddSeconds (cacheDuration);
  78. else
  79. slidindExpiraion = new TimeSpan (0, 0, cacheDuration);
  80. }
  81. string [] dependencies;
  82. if (cacheKeyDependency.Length > 0)
  83. dependencies = new string [] { cacheKeyDependency };
  84. else
  85. dependencies = new string [] { };
  86. DataCache.Add (key, o,
  87. new CacheDependency (new string [] { }, dependencies),
  88. absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
  89. }
  90. static Cache DataCache
  91. {
  92. get {
  93. if (HttpContext.Current != null)
  94. return HttpContext.Current.InternalCache;
  95. throw new InvalidOperationException ("HttpContext.Current is null.");
  96. }
  97. }
  98. string GetKeyFromParameters (string methodName, ParameterCollection parameters)
  99. {
  100. StringBuilder sb = new StringBuilder (methodName);
  101. for (int i = 0; i < parameters.Count; i++) {
  102. sb.Append (parameters [i].Name);
  103. sb.Append (parameters [i].GetValue (context, owner));
  104. }
  105. return sb.ToString ();
  106. }
  107. }
  108. }
  109. #endif