CacheDependency.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #if !TARGET_JVM
  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. #endif
  130. if (cachekeys != null)
  131. foreach (string key in cachekeys)
  132. sb.AppendFormat ("_{0}", key);
  133. return sb.ToString ();
  134. }
  135. #endif
  136. #if !TARGET_JVM
  137. void OnChanged (object sender, FileSystemEventArgs args)
  138. {
  139. if (DateTime.Now < start)
  140. return;
  141. hasChanged = true;
  142. DisposeWatchers ();
  143. if (cache != null)
  144. cache.CheckExpiration ();
  145. }
  146. void DisposeWatchers ()
  147. {
  148. lock (locker) {
  149. if (watchers != null) {
  150. foreach (FileSystemWatcher w in watchers)
  151. if (w != null)
  152. w.Dispose ();
  153. }
  154. watchers = null;
  155. }
  156. }
  157. #else
  158. void DisposeWatchers ()
  159. {
  160. }
  161. #endif
  162. public void Dispose ()
  163. {
  164. DisposeWatchers ();
  165. if (dependency != null)
  166. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  167. cache = null;
  168. }
  169. internal void SetCache (Cache c)
  170. {
  171. cache = c;
  172. #if NET_2_0
  173. used = c != null;
  174. #endif
  175. }
  176. #if NET_2_0
  177. internal bool IsUsed {
  178. get { return used; }
  179. }
  180. internal DateTime Start {
  181. get { return start; }
  182. set { start = value; }
  183. }
  184. #endif
  185. public bool HasChanged {
  186. get {
  187. if (hasChanged)
  188. return true;
  189. if (DateTime.Now < start)
  190. return false;
  191. if (cache != null && cachekeys != null) {
  192. foreach (string key in cachekeys) {
  193. if (cache.GetKeyLastChange (key) > start) {
  194. hasChanged = true;
  195. break;
  196. }
  197. }
  198. }
  199. if (hasChanged)
  200. DisposeWatchers ();
  201. return hasChanged;
  202. }
  203. }
  204. void OnChildDependencyChanged (object o, EventArgs a)
  205. {
  206. hasChanged = true;
  207. OnDependencyChanged ();
  208. }
  209. void OnDependencyChanged ()
  210. {
  211. if (DependencyChanged != null)
  212. DependencyChanged (this, null);
  213. }
  214. #if NET_2_0
  215. internal void SignalDependencyChanged ()
  216. {
  217. OnDependencyChanged ();
  218. }
  219. #endif
  220. internal event EventHandler DependencyChanged;
  221. }
  222. }