CacheDependency.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if (DateTime.Now < start)
  138. return;
  139. hasChanged = true;
  140. #if NET_2_0
  141. utcLastModified = DateTime.UtcNow;
  142. #endif
  143. DisposeWatchers ();
  144. if (cache != null)
  145. cache.CheckExpiration ();
  146. }
  147. void DisposeWatchers ()
  148. {
  149. lock (locker) {
  150. if (watchers != null) {
  151. foreach (FileSystemWatcher w in watchers)
  152. if (w != null)
  153. w.Dispose ();
  154. }
  155. watchers = null;
  156. }
  157. }
  158. public void Dispose ()
  159. {
  160. DependencyDispose ();
  161. }
  162. #if NET_2_0
  163. protected virtual
  164. #endif
  165. void DependencyDispose ()
  166. {
  167. DisposeWatchers ();
  168. if (dependency != null)
  169. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  170. cache = null;
  171. }
  172. internal void SetCache (Cache c)
  173. {
  174. cache = c;
  175. #if NET_2_0
  176. used = c != null;
  177. #endif
  178. }
  179. #if NET_2_0
  180. protected internal void FinishInit ()
  181. {
  182. utcLastModified = DateTime.UtcNow;
  183. }
  184. internal bool IsUsed {
  185. get { return used; }
  186. }
  187. internal DateTime Start {
  188. get { return start; }
  189. set { start = value; }
  190. }
  191. public DateTime UtcLastModified {
  192. get {
  193. return utcLastModified;
  194. }
  195. }
  196. protected void SetUtcLastModified (DateTime utcLastModified)
  197. {
  198. this.utcLastModified = utcLastModified;
  199. }
  200. #endif
  201. public bool HasChanged {
  202. get {
  203. if (hasChanged)
  204. return true;
  205. if (DateTime.Now < start)
  206. return false;
  207. if (cache != null && cachekeys != null) {
  208. foreach (string key in cachekeys) {
  209. if (cache.GetKeyLastChange (key) > start) {
  210. hasChanged = true;
  211. break;
  212. }
  213. }
  214. }
  215. if (hasChanged)
  216. DisposeWatchers ();
  217. return hasChanged;
  218. }
  219. }
  220. void OnChildDependencyChanged (object o, EventArgs e)
  221. {
  222. hasChanged = true;
  223. OnDependencyChanged (o, e);
  224. }
  225. void OnDependencyChanged (object sender, EventArgs e)
  226. {
  227. if (DependencyChanged != null)
  228. DependencyChanged (sender, e);
  229. }
  230. #if NET_2_0
  231. protected
  232. #else
  233. internal
  234. #endif
  235. void NotifyDependencyChanged (object sender, EventArgs e)
  236. {
  237. OnDependencyChanged (sender, e);
  238. }
  239. internal event EventHandler DependencyChanged;
  240. }
  241. }