CacheDependency.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.ComponentModel;
  31. using System.IO;
  32. using System.Security.Permissions;
  33. #if NET_2_0
  34. using System.Text;
  35. #endif
  36. namespace System.Web.Caching
  37. {
  38. #if NET_2_0
  39. // CAS
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public class CacheDependency: IDisposable {
  43. #else
  44. // CAS - no InheritanceDemand here as the class is sealed
  45. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. public sealed class CacheDependency: IDisposable {
  47. #endif
  48. static readonly object dependencyChangedEvent = new object ();
  49. string[] cachekeys;
  50. CacheDependency dependency;
  51. DateTime start;
  52. Cache cache;
  53. FileSystemWatcher[] watchers;
  54. bool hasChanged;
  55. #if NET_2_0
  56. bool used;
  57. DateTime utcLastModified;
  58. #endif
  59. object locker = new object ();
  60. EventHandlerList events = new EventHandlerList ();
  61. internal event EventHandler DependencyChanged {
  62. add { events.AddHandler (dependencyChangedEvent, value); }
  63. remove { events.RemoveHandler (dependencyChangedEvent, value); }
  64. }
  65. #if NET_2_0
  66. public CacheDependency (): this (null, null, null, DateTime.Now)
  67. {
  68. }
  69. #endif
  70. public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
  71. {
  72. }
  73. public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
  74. {
  75. }
  76. public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
  77. {
  78. }
  79. public CacheDependency (string [] filenames, DateTime start)
  80. : this (filenames, null, null, start)
  81. {
  82. }
  83. public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
  84. {
  85. }
  86. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
  87. {
  88. }
  89. public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
  90. {
  91. }
  92. public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
  93. {
  94. if (filenames != null) {
  95. watchers = new FileSystemWatcher [filenames.Length];
  96. for (int n=0; n<filenames.Length; n++) {
  97. FileSystemWatcher watcher = new FileSystemWatcher ();
  98. if (Directory.Exists (filenames [n])) {
  99. watcher.Path = filenames [n];
  100. } else {
  101. string parentPath = Path.GetDirectoryName (filenames [n]);
  102. if (parentPath != null && Directory.Exists (parentPath)) {
  103. watcher.Path = parentPath;
  104. watcher.Filter = Path.GetFileName (filenames [n]);
  105. } else
  106. continue;
  107. }
  108. watcher.NotifyFilter |= NotifyFilters.Size;
  109. watcher.Created += new FileSystemEventHandler (OnChanged);
  110. watcher.Changed += new FileSystemEventHandler (OnChanged);
  111. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  112. watcher.Renamed += new RenamedEventHandler (OnChanged);
  113. watcher.EnableRaisingEvents = true;
  114. watchers [n] = watcher;
  115. }
  116. }
  117. this.cachekeys = cachekeys;
  118. this.dependency = dependency;
  119. if (dependency != null)
  120. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  121. this.start = start;
  122. #if NET_2_0
  123. FinishInit ();
  124. #endif
  125. }
  126. #if NET_2_0
  127. public virtual string GetUniqueID ()
  128. {
  129. StringBuilder sb = new StringBuilder ();
  130. lock (locker) {
  131. if (watchers != null)
  132. foreach (FileSystemWatcher fsw in watchers)
  133. if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
  134. sb.AppendFormat ("_{0}", fsw.Path);
  135. }
  136. if (cachekeys != null)
  137. foreach (string key in cachekeys)
  138. sb.AppendFormat ("_{0}", key);
  139. return sb.ToString ();
  140. }
  141. #endif
  142. void OnChanged (object sender, FileSystemEventArgs args)
  143. {
  144. OnDependencyChanged (sender, args);
  145. }
  146. bool DoOnChanged ()
  147. {
  148. if (DateTime.Now < start)
  149. return false;
  150. hasChanged = true;
  151. #if NET_2_0
  152. utcLastModified = DateTime.UtcNow;
  153. #endif
  154. DisposeWatchers ();
  155. if (cache != null)
  156. cache.CheckDependencies ();
  157. return true;
  158. }
  159. void DisposeWatchers ()
  160. {
  161. lock (locker) {
  162. if (watchers != null) {
  163. foreach (FileSystemWatcher w in watchers)
  164. if (w != null)
  165. w.Dispose ();
  166. }
  167. watchers = null;
  168. }
  169. }
  170. public void Dispose ()
  171. {
  172. DependencyDispose ();
  173. }
  174. #if NET_2_0
  175. internal virtual void DependencyDisposeInternal ()
  176. {
  177. }
  178. #endif
  179. #if NET_2_0
  180. protected virtual
  181. #endif
  182. void DependencyDispose ()
  183. {
  184. #if NET_2_0
  185. DependencyDisposeInternal ();
  186. #endif
  187. DisposeWatchers ();
  188. if (dependency != null) {
  189. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  190. dependency.Dispose ();
  191. }
  192. cache = null;
  193. }
  194. internal void SetCache (Cache c)
  195. {
  196. cache = c;
  197. #if NET_2_0
  198. used = c != null;
  199. #endif
  200. }
  201. #if NET_2_0
  202. protected internal void FinishInit ()
  203. {
  204. utcLastModified = DateTime.UtcNow;
  205. }
  206. internal bool IsUsed {
  207. get { return used; }
  208. }
  209. internal DateTime Start {
  210. get { return start; }
  211. set { start = value; }
  212. }
  213. public DateTime UtcLastModified {
  214. get {
  215. return utcLastModified;
  216. }
  217. }
  218. protected void SetUtcLastModified (DateTime utcLastModified)
  219. {
  220. this.utcLastModified = utcLastModified;
  221. }
  222. #endif
  223. public bool HasChanged {
  224. get {
  225. if (hasChanged)
  226. return true;
  227. if (DateTime.Now < start)
  228. return false;
  229. if (cache != null && cachekeys != null) {
  230. foreach (string key in cachekeys) {
  231. if (cache.GetKeyLastChange (key) > start) {
  232. hasChanged = true;
  233. break;
  234. }
  235. }
  236. }
  237. if (hasChanged)
  238. DisposeWatchers ();
  239. return hasChanged;
  240. }
  241. }
  242. void OnChildDependencyChanged (object o, EventArgs e)
  243. {
  244. hasChanged = true;
  245. OnDependencyChanged (o, e);
  246. }
  247. void OnDependencyChanged (object sender, EventArgs e)
  248. {
  249. if (!DoOnChanged ())
  250. return;
  251. EventHandler eh = events [dependencyChangedEvent] as EventHandler;
  252. if (eh != null)
  253. eh (sender, e);
  254. }
  255. #if NET_2_0
  256. protected
  257. #else
  258. internal
  259. #endif
  260. void NotifyDependencyChanged (object sender, EventArgs e)
  261. {
  262. OnDependencyChanged (sender, e);
  263. }
  264. }
  265. }