CacheDependency.cs 7.1 KB

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