CacheDependency.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // System.Web.Caching.CacheDependency
  3. //
  4. // Authors:
  5. // Patrik Torstensson
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Web;
  33. namespace System.Web.Caching
  34. {
  35. internal class CacheDependencyChangedArgs : EventArgs
  36. {
  37. string key;
  38. public CacheDependencyChangedArgs (string key)
  39. {
  40. this.key = key;
  41. }
  42. public string Key {
  43. get { return key; }
  44. }
  45. }
  46. internal delegate void CacheDependencyChangedHandler (object sender, CacheDependencyChangedArgs args);
  47. public sealed class CacheDependency : IDisposable
  48. {
  49. static string [] noStrings = new string [0];
  50. static CacheDependency noDependency = new CacheDependency ();
  51. DateTime start;
  52. bool changed;
  53. bool disposed;
  54. CacheEntry [] entries;
  55. CacheItemRemovedCallback removedDelegate;
  56. FileSystemWatcher [] watchers;
  57. private CacheDependency ()
  58. {
  59. }
  60. public CacheDependency (string filename)
  61. : this (filename, DateTime.MaxValue)
  62. {
  63. }
  64. public CacheDependency (string filename, DateTime start)
  65. : this (new string [] {filename}, null, null, start)
  66. {
  67. }
  68. public CacheDependency (string [] filenames)
  69. : this (filenames, null, null, DateTime.MaxValue)
  70. {
  71. }
  72. public CacheDependency (string [] filenames, DateTime start)
  73. : this (filenames, null, null, start)
  74. {
  75. }
  76. public CacheDependency (string [] filenames, string [] cachekeys)
  77. : this (filenames, cachekeys, null, DateTime.MaxValue)
  78. {
  79. }
  80. public CacheDependency (string [] filenames, string [] cachekeys, DateTime start)
  81. : this (filenames, cachekeys, null, start)
  82. {
  83. }
  84. public CacheDependency (string [] filenames, string [] cachekeys, CacheDependency dependency)
  85. : this (filenames, cachekeys, dependency, DateTime.MaxValue)
  86. {
  87. }
  88. public CacheDependency (string [] filenames,
  89. string [] cachekeys,
  90. CacheDependency dependency,
  91. DateTime start)
  92. {
  93. Cache cache = HttpRuntime.Cache;
  94. this.start = start;
  95. if (filenames == null)
  96. filenames = noStrings;
  97. foreach (string file in filenames) {
  98. if (file == null)
  99. throw new ArgumentNullException ("filenames");
  100. }
  101. if (cachekeys == null)
  102. cachekeys = noStrings;
  103. int missing_keys = 0;
  104. foreach (string ck in cachekeys) {
  105. if (ck == null)
  106. throw new ArgumentNullException ("cachekeys");
  107. if (cache.GetEntry (ck) == null)
  108. missing_keys++;
  109. }
  110. if (dependency == null)
  111. dependency = noDependency;
  112. this.changed = dependency.changed;
  113. if (changed == true)
  114. return;
  115. int nentries = cachekeys.Length + ((dependency.entries == null) ? 0 :
  116. dependency.entries.Length) - missing_keys;
  117. if (nentries != 0) {
  118. this.removedDelegate = new CacheItemRemovedCallback (CacheItemRemoved);
  119. this.entries = new CacheEntry [nentries];
  120. int i = 0;
  121. if (dependency.entries != null) {
  122. foreach (CacheEntry entry in dependency.entries) {
  123. entry._onRemoved += removedDelegate;
  124. entries [i++] = entry;
  125. }
  126. }
  127. for (int c=0; c<cachekeys.Length; c++) {
  128. CacheEntry entry = cache.GetEntry (cachekeys [c]);
  129. if (entry == null)
  130. continue;
  131. entry._onRemoved += removedDelegate;
  132. entries [i++] = entry;
  133. }
  134. }
  135. if (filenames.Length > 0) {
  136. watchers = new FileSystemWatcher [filenames.Length];
  137. for (int i=0; i<filenames.Length; i++)
  138. watchers [i] = CreateWatcher (filenames [i]);
  139. }
  140. }
  141. private FileSystemWatcher CreateWatcher (string file)
  142. {
  143. FileSystemWatcher watcher = new FileSystemWatcher ();
  144. watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
  145. watcher.Filter = Path.GetFileName (file);
  146. watcher.Changed += new FileSystemEventHandler (OnFileChanged);
  147. watcher.Created += new FileSystemEventHandler (OnFileChanged);
  148. watcher.Deleted += new FileSystemEventHandler (OnFileChanged);
  149. watcher.EnableRaisingEvents = true;
  150. return watcher;
  151. }
  152. private void OnFileChanged (object sender, FileSystemEventArgs e)
  153. {
  154. OnChanged (sender, e);
  155. }
  156. void CacheItemRemoved (string key, object value, CacheItemRemovedReason reason)
  157. {
  158. OnChanged (this, EventArgs.Empty);
  159. }
  160. void OnChanged (object sender, EventArgs args)
  161. {
  162. if (changed || disposed)
  163. return;
  164. changed = true;
  165. if (Changed != null)
  166. Changed (this, new CacheDependencyChangedArgs (null));
  167. }
  168. public void Dispose ()
  169. {
  170. for (int i=0; i<watchers.Length; i++)
  171. watchers [i].Dispose ();
  172. }
  173. public bool HasChanged
  174. {
  175. get { return changed; }
  176. }
  177. internal CacheEntry [] GetCacheEntries ()
  178. {
  179. return entries;
  180. }
  181. internal event CacheDependencyChangedHandler Changed;
  182. }
  183. }