CacheDependency.cs 4.8 KB

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