CacheDependency.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Created += new FileSystemEventHandler (OnChanged);
  101. watcher.Changed += new FileSystemEventHandler (OnChanged);
  102. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  103. watcher.Renamed += new RenamedEventHandler (OnChanged);
  104. watcher.EnableRaisingEvents = true;
  105. watchers [n] = watcher;
  106. }
  107. }
  108. this.cachekeys = cachekeys;
  109. this.dependency = dependency;
  110. if (dependency != null)
  111. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  112. this.start = start;
  113. }
  114. #if NET_2_0
  115. public virtual string GetUniqueID ()
  116. {
  117. StringBuilder sb = new StringBuilder ();
  118. lock (locker) {
  119. if (watchers != null)
  120. foreach (FileSystemWatcher fsw in watchers)
  121. if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
  122. sb.AppendFormat ("_{0}", fsw.Path);
  123. }
  124. if (cachekeys != null)
  125. foreach (string key in cachekeys)
  126. sb.AppendFormat ("_{0}", key);
  127. return sb.ToString ();
  128. }
  129. #endif
  130. void OnChanged (object sender, FileSystemEventArgs args)
  131. {
  132. if (DateTime.Now < start)
  133. return;
  134. hasChanged = true;
  135. DisposeWatchers ();
  136. if (cache != null)
  137. cache.CheckExpiration ();
  138. }
  139. void DisposeWatchers ()
  140. {
  141. lock (locker) {
  142. if (watchers != null) {
  143. foreach (FileSystemWatcher w in watchers)
  144. if (w != null)
  145. w.Dispose ();
  146. }
  147. watchers = null;
  148. }
  149. }
  150. public void Dispose ()
  151. {
  152. DisposeWatchers ();
  153. if (dependency != null)
  154. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  155. cache = null;
  156. }
  157. internal void SetCache (Cache c)
  158. {
  159. cache = c;
  160. #if NET_2_0
  161. used = c != null;
  162. #endif
  163. }
  164. #if NET_2_0
  165. internal bool IsUsed {
  166. get { return used; }
  167. }
  168. internal DateTime Start {
  169. get { return start; }
  170. set { start = value; }
  171. }
  172. #endif
  173. public bool HasChanged {
  174. get {
  175. if (hasChanged)
  176. return true;
  177. if (DateTime.Now < start)
  178. return false;
  179. if (cache != null && cachekeys != null) {
  180. foreach (string key in cachekeys) {
  181. if (cache.GetKeyLastChange (key) > start) {
  182. hasChanged = true;
  183. break;
  184. }
  185. }
  186. }
  187. if (hasChanged)
  188. DisposeWatchers ();
  189. return hasChanged;
  190. }
  191. }
  192. void OnChildDependencyChanged (object o, EventArgs a)
  193. {
  194. hasChanged = true;
  195. OnDependencyChanged ();
  196. }
  197. void OnDependencyChanged ()
  198. {
  199. if (DependencyChanged != null)
  200. DependencyChanged (this, null);
  201. }
  202. #if NET_2_0
  203. internal void SignalDependencyChanged ()
  204. {
  205. OnDependencyChanged ();
  206. }
  207. #endif
  208. internal event EventHandler DependencyChanged;
  209. }
  210. }