CacheDependency.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Web.Caching.CachedDependency
  3. //
  4. // Author(s):
  5. // Lluis Sanchez ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.IO;
  31. using System.Security.Permissions;
  32. namespace System.Web.Caching
  33. {
  34. #if NET_2_0
  35. // CAS
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public class CacheDependency: IDisposable {
  39. #else
  40. // CAS - no InheritanceDemand here as the class is sealed
  41. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public sealed class CacheDependency: IDisposable {
  43. #endif
  44. string[] cachekeys;
  45. CacheDependency dependency;
  46. DateTime start;
  47. Cache cache;
  48. #if !TARGET_JVM
  49. FileSystemWatcher[] watchers;
  50. #endif
  51. bool hasChanged;
  52. object locker = new object ();
  53. public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
  54. {
  55. }
  56. public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
  57. {
  58. }
  59. public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
  60. {
  61. }
  62. public CacheDependency (string [] filenames, DateTime start)
  63. : this (filenames, null, null, start)
  64. {
  65. }
  66. public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
  67. {
  68. }
  69. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
  70. {
  71. }
  72. public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
  73. {
  74. }
  75. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
  76. {
  77. #if !TARGET_JVM
  78. if (filenames != null) {
  79. watchers = new FileSystemWatcher [filenames.Length];
  80. for (int n=0; n<filenames.Length; n++) {
  81. FileSystemWatcher watcher = new FileSystemWatcher ();
  82. if (Directory.Exists (filenames [n])) {
  83. watcher.Path = filenames [n];
  84. } else {
  85. string parentPath = Path.GetDirectoryName (filenames [n]);
  86. if (parentPath != null && Directory.Exists (parentPath)) {
  87. watcher.Path = parentPath;
  88. watcher.Filter = Path.GetFileName (filenames [n]);
  89. } else
  90. continue;
  91. }
  92. watcher.Created += new FileSystemEventHandler (OnChanged);
  93. watcher.Changed += new FileSystemEventHandler (OnChanged);
  94. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  95. watcher.Renamed += new RenamedEventHandler (OnChanged);
  96. watcher.EnableRaisingEvents = true;
  97. watchers [n] = watcher;
  98. }
  99. }
  100. #endif
  101. this.cachekeys = cachekeys;
  102. this.dependency = dependency;
  103. if (dependency != null)
  104. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  105. this.start = start;
  106. }
  107. void OnChanged (object sender, FileSystemEventArgs args)
  108. {
  109. if (DateTime.Now < start)
  110. return;
  111. hasChanged = true;
  112. DisposeWatchers ();
  113. if (cache != null)
  114. cache.CheckExpiration ();
  115. }
  116. #if TARGET_JVM
  117. void DisposeWatchers ()
  118. {
  119. }
  120. #else
  121. void DisposeWatchers ()
  122. {
  123. lock (locker) {
  124. if (watchers != null) {
  125. foreach (FileSystemWatcher w in watchers)
  126. if (w != null)
  127. w.Dispose ();
  128. }
  129. watchers = null;
  130. }
  131. }
  132. #endif
  133. public void Dispose ()
  134. {
  135. DisposeWatchers ();
  136. if (dependency != null)
  137. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  138. cache = null;
  139. }
  140. internal void SetCache (Cache c)
  141. {
  142. cache = c;
  143. }
  144. public bool HasChanged {
  145. get {
  146. if (hasChanged)
  147. return true;
  148. if (DateTime.Now < start)
  149. return false;
  150. if (cache != null) {
  151. if (cachekeys != null) {
  152. foreach (string key in cachekeys) {
  153. if (cache.GetKeyLastChange (key) > start) {
  154. hasChanged = true;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. if (hasChanged)
  161. DisposeWatchers ();
  162. return hasChanged;
  163. }
  164. }
  165. void OnChildDependencyChanged (object o, EventArgs a)
  166. {
  167. hasChanged = true;
  168. OnDependencyChanged ();
  169. }
  170. void OnDependencyChanged ()
  171. {
  172. if (DependencyChanged != null)
  173. DependencyChanged (this, null);
  174. }
  175. internal event EventHandler DependencyChanged;
  176. }
  177. }