CacheDependency.cs 7.1 KB

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