CacheDependency.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace System.Web.Caching
  32. {
  33. public sealed class CacheDependency: IDisposable
  34. {
  35. string[] cachekeys;
  36. CacheDependency dependency;
  37. DateTime start;
  38. Cache cache;
  39. #if !TARGET_JVM
  40. FileSystemWatcher[] watchers;
  41. #endif
  42. bool hasChanged;
  43. object locker = new object ();
  44. public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
  45. {
  46. }
  47. public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
  48. {
  49. }
  50. public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
  51. {
  52. }
  53. public CacheDependency (string [] filenames, DateTime start)
  54. : this (filenames, null, null, start)
  55. {
  56. }
  57. public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
  58. {
  59. }
  60. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
  61. {
  62. }
  63. public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
  64. {
  65. }
  66. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
  67. {
  68. #if !TARGET_JVM
  69. if (filenames != null) {
  70. watchers = new FileSystemWatcher [filenames.Length];
  71. for (int n=0; n<filenames.Length; n++) {
  72. FileSystemWatcher watcher = new FileSystemWatcher ();
  73. if (Directory.Exists (filenames [n])) {
  74. watcher.Path = filenames [n];
  75. } else {
  76. string parentPath = Path.GetDirectoryName (filenames [n]);
  77. if (parentPath != null && Directory.Exists (parentPath)) {
  78. watcher.Path = parentPath;
  79. watcher.Filter = Path.GetFileName (filenames [n]);
  80. } else
  81. continue;
  82. }
  83. watcher.Created += new FileSystemEventHandler (OnChanged);
  84. watcher.Changed += new FileSystemEventHandler (OnChanged);
  85. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  86. watcher.Renamed += new RenamedEventHandler (OnChanged);
  87. watcher.EnableRaisingEvents = true;
  88. watchers [n] = watcher;
  89. }
  90. }
  91. #endif
  92. this.cachekeys = cachekeys;
  93. this.dependency = dependency;
  94. if (dependency != null)
  95. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  96. this.start = start;
  97. }
  98. void OnChanged (object sender, FileSystemEventArgs args)
  99. {
  100. if (DateTime.Now < start)
  101. return;
  102. hasChanged = true;
  103. DisposeWatchers ();
  104. if (cache != null)
  105. cache.CheckExpiration ();
  106. }
  107. #if TARGET_JVM
  108. void DisposeWatchers ()
  109. {
  110. }
  111. #else
  112. void DisposeWatchers ()
  113. {
  114. lock (locker) {
  115. if (watchers != null) {
  116. foreach (FileSystemWatcher w in watchers)
  117. if (w != null)
  118. w.Dispose ();
  119. }
  120. watchers = null;
  121. }
  122. }
  123. #endif
  124. public void Dispose ()
  125. {
  126. DisposeWatchers ();
  127. if (dependency != null)
  128. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  129. cache = null;
  130. }
  131. internal void SetCache (Cache c)
  132. {
  133. cache = c;
  134. }
  135. public bool HasChanged {
  136. get {
  137. if (hasChanged)
  138. return true;
  139. if (DateTime.Now < start)
  140. return false;
  141. if (cache != null) {
  142. if (cachekeys != null) {
  143. foreach (string key in cachekeys) {
  144. if (cache.GetKeyLastChange (key) > start) {
  145. hasChanged = true;
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. if (hasChanged)
  152. DisposeWatchers ();
  153. return hasChanged;
  154. }
  155. }
  156. void OnChildDependencyChanged (object o, EventArgs a)
  157. {
  158. hasChanged = true;
  159. OnDependencyChanged ();
  160. }
  161. void OnDependencyChanged ()
  162. {
  163. if (DependencyChanged != null)
  164. DependencyChanged (this, null);
  165. }
  166. internal event EventHandler DependencyChanged;
  167. }
  168. }