CacheDependency.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.Collections.Generic;
  31. using System.ComponentModel;
  32. using System.IO;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. namespace System.Web.Caching
  36. {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. public class CacheDependency: IDisposable
  41. {
  42. static readonly object dependencyChangedEvent = new object ();
  43. string[] cachekeys;
  44. CacheDependency dependency;
  45. DateTime start;
  46. Cache cache;
  47. FileSystemWatcher[] watchers;
  48. static readonly bool useSharedWatchers = Environment.GetEnvironmentVariable ("MONO_SYSTEMWEB_CACHEDEPENDENCY_SHARED_FSW") != null;
  49. static readonly Dictionary<string, FileSystemWatcher> sharedWatchers = new Dictionary<string, FileSystemWatcher> ();
  50. bool hasChanged;
  51. bool used;
  52. DateTime utcLastModified;
  53. static readonly object locker = new object ();
  54. EventHandlerList events = new EventHandlerList ();
  55. internal event EventHandler DependencyChanged {
  56. add { events.AddHandler (dependencyChangedEvent, value); }
  57. remove { events.RemoveHandler (dependencyChangedEvent, value); }
  58. }
  59. protected
  60. CacheDependency (): this (null, null, null, DateTime.Now)
  61. {
  62. }
  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. int flen = filenames != null ? filenames.Length : 0;
  88. if (flen > 0) {
  89. watchers = new FileSystemWatcher [flen];
  90. string filename;
  91. for (int n = 0; n < flen; n++) {
  92. filename = filenames [n];
  93. if (String.IsNullOrEmpty (filename))
  94. continue;
  95. string path = null;
  96. string filter = null;
  97. if (Directory.Exists (filename))
  98. path = filename;
  99. else {
  100. string parentPath = Path.GetDirectoryName (filename);
  101. if (parentPath != null && Directory.Exists (parentPath)) {
  102. path = parentPath;
  103. filter = Path.GetFileName (filename);
  104. } else
  105. continue;
  106. }
  107. lock (locker) {
  108. FileSystemWatcher watcher;
  109. if (useSharedWatchers) {
  110. if (!sharedWatchers.TryGetValue (path, out watcher)) {
  111. watcher = new FileSystemWatcher ();
  112. watcher.Path = path;
  113. watcher.NotifyFilter |= NotifyFilters.Size;
  114. watcher.Created += new FileSystemEventHandler ((s, e) => { if (filter == null || e.Name == filter) OnChanged (s, e); });
  115. watcher.Changed += new FileSystemEventHandler ((s, e) => { if (filter == null || e.Name == filter) OnChanged (s, e); });
  116. watcher.Deleted += new FileSystemEventHandler ((s, e) => { if (filter == null || e.Name == filter) OnChanged (s, e); });
  117. watcher.Renamed += new RenamedEventHandler ((s, e) => { if (filter == null || e.OldName == filter) OnChanged (s, e); });
  118. watcher.EnableRaisingEvents = true;
  119. sharedWatchers [path] = watcher;
  120. }
  121. } else {
  122. watcher = new FileSystemWatcher ();
  123. watcher.Path = path;
  124. if (filter != null)
  125. watcher.Filter = filter;
  126. watcher.NotifyFilter |= NotifyFilters.Size;
  127. watcher.Created += new FileSystemEventHandler (OnChanged);
  128. watcher.Changed += new FileSystemEventHandler (OnChanged);
  129. watcher.Deleted += new FileSystemEventHandler (OnChanged);
  130. watcher.Renamed += new RenamedEventHandler (OnChanged);
  131. watcher.EnableRaisingEvents = true;
  132. watchers [n] = watcher;
  133. }
  134. }
  135. }
  136. }
  137. this.cachekeys = cachekeys;
  138. this.dependency = dependency;
  139. if (dependency != null)
  140. dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
  141. this.start = start;
  142. FinishInit ();
  143. }
  144. public virtual string GetUniqueID ()
  145. {
  146. var sb = new StringBuilder ();
  147. lock (locker) {
  148. FileSystemWatcher[] watcherList;
  149. if (useSharedWatchers) {
  150. watcherList = new FileSystemWatcher[sharedWatchers.Count];
  151. sharedWatchers.Values.CopyTo (watcherList, 0);
  152. } else {
  153. watcherList = watchers;
  154. }
  155. if (watcherList != null)
  156. foreach (FileSystemWatcher fsw in watcherList)
  157. if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
  158. sb.Append ("_" + fsw.Path);
  159. }
  160. if (cachekeys != null)
  161. foreach (string key in cachekeys)
  162. sb.AppendFormat ("_" + key);
  163. return sb.ToString ();
  164. }
  165. void OnChanged (object sender, FileSystemEventArgs args)
  166. {
  167. OnDependencyChanged (sender, args);
  168. }
  169. bool DoOnChanged ()
  170. {
  171. DateTime now = DateTime.Now;
  172. if (now < start)
  173. return false;
  174. hasChanged = true;
  175. utcLastModified = now.ToUniversalTime ();
  176. DisposeWatchers ();
  177. if (cache != null)
  178. cache.CheckDependencies ();
  179. return true;
  180. }
  181. void DisposeWatchers ()
  182. {
  183. if (useSharedWatchers) return;
  184. lock (locker) {
  185. if (watchers != null) {
  186. foreach (FileSystemWatcher w in watchers)
  187. if (w != null)
  188. w.Dispose ();
  189. }
  190. watchers = null;
  191. }
  192. }
  193. public void Dispose ()
  194. {
  195. DependencyDispose ();
  196. }
  197. internal virtual void DependencyDisposeInternal ()
  198. {
  199. }
  200. protected virtual void DependencyDispose ()
  201. {
  202. DependencyDisposeInternal ();
  203. DisposeWatchers ();
  204. if (dependency != null) {
  205. dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
  206. dependency.Dispose ();
  207. }
  208. cache = null;
  209. }
  210. internal void SetCache (Cache c)
  211. {
  212. cache = c;
  213. used = c != null;
  214. }
  215. protected internal void FinishInit ()
  216. {
  217. utcLastModified = DateTime.UtcNow;
  218. }
  219. internal bool IsUsed {
  220. get { return used; }
  221. }
  222. internal DateTime Start {
  223. get { return start; }
  224. set { start = value; }
  225. }
  226. public DateTime UtcLastModified {
  227. get {
  228. return utcLastModified;
  229. }
  230. }
  231. protected void SetUtcLastModified (DateTime utcLastModified)
  232. {
  233. this.utcLastModified = utcLastModified;
  234. }
  235. public bool HasChanged {
  236. get {
  237. if (hasChanged)
  238. return true;
  239. if (DateTime.Now < start)
  240. return false;
  241. if (cache != null && cachekeys != null) {
  242. foreach (string key in cachekeys) {
  243. if (cache.GetKeyLastChange (key) > start) {
  244. hasChanged = true;
  245. break;
  246. }
  247. }
  248. }
  249. if (hasChanged)
  250. DisposeWatchers ();
  251. return hasChanged;
  252. }
  253. }
  254. void OnChildDependencyChanged (object o, EventArgs e)
  255. {
  256. hasChanged = true;
  257. OnDependencyChanged (o, e);
  258. }
  259. void OnDependencyChanged (object sender, EventArgs e)
  260. {
  261. if (!DoOnChanged ())
  262. return;
  263. EventHandler eh = events [dependencyChangedEvent] as EventHandler;
  264. if (eh != null)
  265. eh (sender, e);
  266. }
  267. protected void NotifyDependencyChanged (object sender, EventArgs e)
  268. {
  269. OnDependencyChanged (sender, e);
  270. }
  271. }
  272. }