CacheDependency.cs 7.0 KB

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