CacheDependency.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. bool changed;
  52. bool disposed;
  53. CacheEntry [] entries;
  54. CacheItemRemovedCallback removedDelegate;
  55. FileSystemWatcher [] watchers;
  56. private CacheDependency ()
  57. {
  58. }
  59. public CacheDependency (string filename)
  60. : this (filename, DateTime.MaxValue)
  61. {
  62. }
  63. public CacheDependency (string filename, DateTime start)
  64. : this (new string [] {filename}, null, null, start)
  65. {
  66. }
  67. public CacheDependency (string [] filenames)
  68. : this (filenames, null, null, DateTime.MaxValue)
  69. {
  70. }
  71. public CacheDependency (string [] filenames, DateTime start)
  72. : this (filenames, null, null, start)
  73. {
  74. }
  75. public CacheDependency (string [] filenames, string [] cachekeys)
  76. : this (filenames, cachekeys, null, DateTime.MaxValue)
  77. {
  78. }
  79. public CacheDependency (string [] filenames, string [] cachekeys, DateTime start)
  80. : this (filenames, cachekeys, null, start)
  81. {
  82. }
  83. public CacheDependency (string [] filenames, string [] cachekeys, CacheDependency dependency)
  84. : this (filenames, cachekeys, dependency, DateTime.MaxValue)
  85. {
  86. }
  87. public CacheDependency (string [] filenames,
  88. string [] cachekeys,
  89. CacheDependency dependency,
  90. DateTime start)
  91. {
  92. Cache cache = HttpRuntime.Cache;
  93. if (filenames == null)
  94. filenames = noStrings;
  95. foreach (string file in filenames) {
  96. if (file == null)
  97. throw new ArgumentNullException ("filenames");
  98. }
  99. if (cachekeys == null)
  100. cachekeys = noStrings;
  101. int missing_keys = 0;
  102. foreach (string ck in cachekeys) {
  103. if (ck == null)
  104. throw new ArgumentNullException ("cachekeys");
  105. if (cache.GetEntry (ck) == null)
  106. missing_keys++;
  107. }
  108. if (dependency == null)
  109. dependency = noDependency;
  110. this.changed = dependency.changed;
  111. if (changed == true)
  112. return;
  113. int nentries = cachekeys.Length + ((dependency.entries == null) ? 0 :
  114. dependency.entries.Length) - missing_keys;
  115. if (nentries != 0) {
  116. this.removedDelegate = new CacheItemRemovedCallback (CacheItemRemoved);
  117. this.entries = new CacheEntry [nentries];
  118. int i = 0;
  119. if (dependency.entries != null) {
  120. foreach (CacheEntry entry in dependency.entries) {
  121. entry._onRemoved += removedDelegate;
  122. entries [i++] = entry;
  123. }
  124. }
  125. for (int c=0; c<cachekeys.Length; c++) {
  126. CacheEntry entry = cache.GetEntry (cachekeys [c]);
  127. if (entry == null)
  128. continue;
  129. entry._onRemoved += removedDelegate;
  130. entries [i++] = entry;
  131. }
  132. }
  133. if (filenames.Length > 0) {
  134. watchers = new FileSystemWatcher [filenames.Length];
  135. for (int i=0; i<filenames.Length; i++)
  136. watchers [i] = CreateWatcher (filenames [i]);
  137. }
  138. }
  139. private FileSystemWatcher CreateWatcher (string file)
  140. {
  141. FileSystemWatcher watcher = new FileSystemWatcher ();
  142. watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
  143. watcher.Filter = Path.GetFileName (file);
  144. watcher.Changed += new FileSystemEventHandler (OnFileChanged);
  145. watcher.Created += new FileSystemEventHandler (OnFileChanged);
  146. watcher.Deleted += new FileSystemEventHandler (OnFileChanged);
  147. watcher.EnableRaisingEvents = true;
  148. return watcher;
  149. }
  150. private void OnFileChanged (object sender, FileSystemEventArgs e)
  151. {
  152. OnChanged (sender, e);
  153. }
  154. void CacheItemRemoved (string key, object value, CacheItemRemovedReason reason)
  155. {
  156. OnChanged (this, EventArgs.Empty);
  157. }
  158. void OnChanged (object sender, EventArgs args)
  159. {
  160. if (changed || disposed)
  161. return;
  162. changed = true;
  163. if (Changed != null)
  164. Changed (this, new CacheDependencyChangedArgs (null));
  165. }
  166. public void Dispose ()
  167. {
  168. for (int i=0; i<watchers.Length; i++)
  169. watchers [i].Dispose ();
  170. }
  171. public bool HasChanged
  172. {
  173. get { return changed; }
  174. }
  175. internal CacheEntry [] GetCacheEntries ()
  176. {
  177. return entries;
  178. }
  179. internal event CacheDependencyChangedHandler Changed;
  180. }
  181. }