CacheDependency.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #if !TARGET_J2EE
  56. FileSystemWatcher [] watchers;
  57. #endif
  58. private CacheDependency ()
  59. {
  60. }
  61. public CacheDependency (string filename)
  62. : this (filename, DateTime.MaxValue)
  63. {
  64. }
  65. public CacheDependency (string filename, DateTime start)
  66. : this (new string [] {filename}, null, null, start)
  67. {
  68. }
  69. public CacheDependency (string [] filenames)
  70. : this (filenames, null, null, DateTime.MaxValue)
  71. {
  72. }
  73. public CacheDependency (string [] filenames, DateTime start)
  74. : this (filenames, null, null, start)
  75. {
  76. }
  77. public CacheDependency (string [] filenames, string [] cachekeys)
  78. : this (filenames, cachekeys, null, DateTime.MaxValue)
  79. {
  80. }
  81. public CacheDependency (string [] filenames, string [] cachekeys, DateTime start)
  82. : this (filenames, cachekeys, null, start)
  83. {
  84. }
  85. public CacheDependency (string [] filenames, string [] cachekeys, CacheDependency dependency)
  86. : this (filenames, cachekeys, dependency, DateTime.MaxValue)
  87. {
  88. }
  89. public CacheDependency (string [] filenames,
  90. string [] cachekeys,
  91. CacheDependency dependency,
  92. DateTime start)
  93. {
  94. Cache cache = HttpRuntime.Cache;
  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 !TARGET_J2EE
  136. if (filenames.Length > 0) {
  137. watchers = new FileSystemWatcher [filenames.Length];
  138. for (int i=0; i<filenames.Length; i++)
  139. watchers [i] = CreateWatcher (filenames [i]);
  140. }
  141. #endif
  142. }
  143. #if !TARGET_J2EE
  144. private FileSystemWatcher CreateWatcher (string file)
  145. {
  146. FileSystemWatcher watcher = new FileSystemWatcher ();
  147. watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
  148. watcher.Filter = Path.GetFileName (file);
  149. watcher.Changed += new FileSystemEventHandler (OnFileChanged);
  150. watcher.Created += new FileSystemEventHandler (OnFileChanged);
  151. watcher.Deleted += new FileSystemEventHandler (OnFileChanged);
  152. watcher.EnableRaisingEvents = true;
  153. return watcher;
  154. }
  155. private void OnFileChanged (object sender, FileSystemEventArgs e)
  156. {
  157. OnChanged (sender, e);
  158. }
  159. #endif
  160. void CacheItemRemoved (string key, object value, CacheItemRemovedReason reason)
  161. {
  162. OnChanged (this, EventArgs.Empty);
  163. }
  164. void OnChanged (object sender, EventArgs args)
  165. {
  166. if (changed || disposed)
  167. return;
  168. changed = true;
  169. if (Changed != null)
  170. Changed (this, new CacheDependencyChangedArgs (null));
  171. }
  172. #if TARGET_J2EE
  173. public void Dispose ()
  174. {
  175. }
  176. #else
  177. public void Dispose ()
  178. {
  179. for (int i=0; i<watchers.Length; i++)
  180. watchers [i].Dispose ();
  181. }
  182. #endif
  183. public bool HasChanged
  184. {
  185. get { return changed; }
  186. }
  187. internal CacheEntry [] GetCacheEntries ()
  188. {
  189. return entries;
  190. }
  191. internal event CacheDependencyChangedHandler Changed;
  192. }
  193. }