CacheDependency.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #if !TARGET_JVM
  52. FileSystemWatcher[] watchers;
  53. #endif
  54. bool hasChanged;
  55. #if NET_2_0
  56. bool used;
  57. #endif
  58. object locker = new object ();
  59. #if NET_2_0
  60. public CacheDependency (): this (null, null, null, DateTime.Now)
  61. {
  62. }
  63. #endif
  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. #if !TARGET_JVM
  89. if (filenames != null) {
  90. watchers = new FileSystemWatcher [filenames.Length];
  91. for (int n=0; n<filenames.Length; n++) {
  92. FileSystemWatcher watcher = new FileSystemWatcher ();
  93. if (Directory.Exists (filenames [n])) {
  94. watcher.Path = filenames [n];
  95. } else {
  96. string parentPath = Path.GetDirectoryName (filenames [n]);
  97. if (parentPath != null && Directory.Exists (parentPath)) {
  98. watcher.Path = parentPath;
  99. watcher.Filter = Path.GetFileName (filenames [n]);
  100. } else
  101. continue;
  102. }
  103. watcher.Created += new FileSystemEventHandler (OnChanged);
  104. watcher.Changed += new FileSystemEventHandler (OnChanged);
  105. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  106. watcher.Renamed += new RenamedEventHandler (OnChanged);
  107. watcher.EnableRaisingEvents = true;
  108. watchers [n] = watcher;
  109. }
  110. }
  111. #endif
  112. this.cachekeys = cachekeys;
  113. this.dependency = dependency;
  114. if (dependency != null)
  115. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  116. this.start = start;
  117. }
  118. #if NET_2_0
  119. public virtual string GetUniqueID ()
  120. {
  121. StringBuilder sb = new StringBuilder ();
  122. lock (locker) {
  123. if (watchers != null)
  124. foreach (FileSystemWatcher fsw in watchers)
  125. if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
  126. sb.AppendFormat ("_{0}", fsw.Path);
  127. }
  128. if (cachekeys != null)
  129. foreach (string key in cachekeys)
  130. sb.AppendFormat ("_{0}", key);
  131. return sb.ToString ();
  132. }
  133. #endif
  134. #if !TARGET_JVM
  135. void OnChanged (object sender, FileSystemEventArgs args)
  136. {
  137. if (DateTime.Now < start)
  138. return;
  139. hasChanged = true;
  140. DisposeWatchers ();
  141. if (cache != null)
  142. cache.CheckExpiration ();
  143. }
  144. void DisposeWatchers ()
  145. {
  146. lock (locker) {
  147. if (watchers != null) {
  148. foreach (FileSystemWatcher w in watchers)
  149. if (w != null)
  150. w.Dispose ();
  151. }
  152. watchers = null;
  153. }
  154. }
  155. #else
  156. void DisposeWatchers ()
  157. {
  158. }
  159. #endif
  160. public void Dispose ()
  161. {
  162. DisposeWatchers ();
  163. if (dependency != null)
  164. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  165. cache = null;
  166. }
  167. internal void SetCache (Cache c)
  168. {
  169. cache = c;
  170. #if NET_2_0
  171. used = c != null;
  172. #endif
  173. }
  174. #if NET_2_0
  175. internal bool IsUsed {
  176. get { return used; }
  177. }
  178. internal DateTime Start {
  179. get { return start; }
  180. set { start = value; }
  181. }
  182. #endif
  183. public bool HasChanged {
  184. get {
  185. if (hasChanged)
  186. return true;
  187. if (DateTime.Now < start)
  188. return false;
  189. if (cache != null && cachekeys != null) {
  190. foreach (string key in cachekeys) {
  191. if (cache.GetKeyLastChange (key) > start) {
  192. hasChanged = true;
  193. break;
  194. }
  195. }
  196. }
  197. if (hasChanged)
  198. DisposeWatchers ();
  199. return hasChanged;
  200. }
  201. }
  202. void OnChildDependencyChanged (object o, EventArgs a)
  203. {
  204. hasChanged = true;
  205. OnDependencyChanged ();
  206. }
  207. void OnDependencyChanged ()
  208. {
  209. if (DependencyChanged != null)
  210. DependencyChanged (this, null);
  211. }
  212. #if NET_2_0
  213. internal void SignalDependencyChanged ()
  214. {
  215. OnDependencyChanged ();
  216. }
  217. #endif
  218. internal event EventHandler DependencyChanged;
  219. }
  220. }