CacheDependency.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. using System.Security.Permissions;
  32. #if NET_2_0
  33. using System.Text;
  34. #endif
  35. namespace System.Web.Caching
  36. {
  37. #if NET_2_0
  38. // CAS
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. public class CacheDependency: IDisposable {
  42. #else
  43. // CAS - no InheritanceDemand here as the class is sealed
  44. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. public sealed class CacheDependency: IDisposable {
  46. #endif
  47. string[] cachekeys;
  48. CacheDependency dependency;
  49. DateTime start;
  50. Cache cache;
  51. FileSystemWatcher[] watchers;
  52. bool hasChanged;
  53. #if NET_2_0
  54. bool used;
  55. #endif
  56. object locker = new object ();
  57. #if NET_2_0
  58. public CacheDependency (): this (null, null, null, DateTime.Now)
  59. {
  60. }
  61. #endif
  62. public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
  63. {
  64. }
  65. public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
  66. {
  67. }
  68. public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
  69. {
  70. }
  71. public CacheDependency (string [] filenames, DateTime start)
  72. : this (filenames, null, null, start)
  73. {
  74. }
  75. public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
  76. {
  77. }
  78. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
  79. {
  80. }
  81. public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
  82. {
  83. }
  84. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
  85. {
  86. if (filenames != null) {
  87. watchers = new FileSystemWatcher [filenames.Length];
  88. for (int n=0; n<filenames.Length; n++) {
  89. FileSystemWatcher watcher = new FileSystemWatcher ();
  90. if (Directory.Exists (filenames [n])) {
  91. watcher.Path = filenames [n];
  92. } else {
  93. string parentPath = Path.GetDirectoryName (filenames [n]);
  94. if (parentPath != null && Directory.Exists (parentPath)) {
  95. watcher.Path = parentPath;
  96. watcher.Filter = Path.GetFileName (filenames [n]);
  97. } else
  98. continue;
  99. }
  100. watcher.NotifyFilter |= NotifyFilters.Size;
  101. watcher.Created += new FileSystemEventHandler (OnChanged);
  102. watcher.Changed += new FileSystemEventHandler (OnChanged);
  103. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  104. watcher.Renamed += new RenamedEventHandler (OnChanged);
  105. watcher.EnableRaisingEvents = true;
  106. watchers [n] = watcher;
  107. }
  108. }
  109. this.cachekeys = cachekeys;
  110. this.dependency = dependency;
  111. if (dependency != null)
  112. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  113. this.start = start;
  114. }
  115. #if NET_2_0
  116. public virtual string GetUniqueID ()
  117. {
  118. StringBuilder sb = new StringBuilder ();
  119. lock (locker) {
  120. if (watchers != null)
  121. foreach (FileSystemWatcher fsw in watchers)
  122. if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
  123. sb.AppendFormat ("_{0}", fsw.Path);
  124. }
  125. if (cachekeys != null)
  126. foreach (string key in cachekeys)
  127. sb.AppendFormat ("_{0}", key);
  128. return sb.ToString ();
  129. }
  130. #endif
  131. void OnChanged (object sender, FileSystemEventArgs args)
  132. {
  133. if (DateTime.Now < start)
  134. return;
  135. hasChanged = true;
  136. DisposeWatchers ();
  137. if (cache != null)
  138. cache.CheckExpiration ();
  139. }
  140. void DisposeWatchers ()
  141. {
  142. lock (locker) {
  143. if (watchers != null) {
  144. foreach (FileSystemWatcher w in watchers)
  145. if (w != null)
  146. w.Dispose ();
  147. }
  148. watchers = null;
  149. }
  150. }
  151. public void Dispose ()
  152. {
  153. DisposeWatchers ();
  154. if (dependency != null)
  155. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  156. cache = null;
  157. }
  158. internal void SetCache (Cache c)
  159. {
  160. cache = c;
  161. #if NET_2_0
  162. used = c != null;
  163. #endif
  164. }
  165. #if NET_2_0
  166. internal bool IsUsed {
  167. get { return used; }
  168. }
  169. internal DateTime Start {
  170. get { return start; }
  171. set { start = value; }
  172. }
  173. #endif
  174. public bool HasChanged {
  175. get {
  176. if (hasChanged)
  177. return true;
  178. if (DateTime.Now < start)
  179. return false;
  180. if (cache != null && cachekeys != null) {
  181. foreach (string key in cachekeys) {
  182. if (cache.GetKeyLastChange (key) > start) {
  183. hasChanged = true;
  184. break;
  185. }
  186. }
  187. }
  188. if (hasChanged)
  189. DisposeWatchers ();
  190. return hasChanged;
  191. }
  192. }
  193. void OnChildDependencyChanged (object o, EventArgs a)
  194. {
  195. hasChanged = true;
  196. OnDependencyChanged ();
  197. }
  198. void OnDependencyChanged ()
  199. {
  200. if (DependencyChanged != null)
  201. DependencyChanged (this, null);
  202. }
  203. #if NET_2_0
  204. internal void SignalDependencyChanged ()
  205. {
  206. OnDependencyChanged ();
  207. }
  208. #endif
  209. internal event EventHandler DependencyChanged;
  210. }
  211. }