2 // System.Web.Caching.CachedDependency
5 // Lluis Sanchez (lluis@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.ComponentModel;
32 using System.Security.Permissions;
35 namespace System.Web.Caching
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public class CacheDependency: IDisposable
42 static readonly object dependencyChangedEvent = new object ();
44 CacheDependency dependency;
47 FileSystemWatcher[] watchers;
50 DateTime utcLastModified;
51 object locker = new object ();
52 EventHandlerList events = new EventHandlerList ();
54 internal event EventHandler DependencyChanged {
55 add { events.AddHandler (dependencyChangedEvent, value); }
56 remove { events.RemoveHandler (dependencyChangedEvent, value); }
59 public CacheDependency (): this (null, null, null, DateTime.Now)
63 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
67 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
71 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
75 public CacheDependency (string [] filenames, DateTime start)
76 : this (filenames, null, null, start)
80 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
84 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
88 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
92 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
94 if (filenames != null) {
95 watchers = new FileSystemWatcher [filenames.Length];
96 for (int n=0; n<filenames.Length; n++) {
97 FileSystemWatcher watcher = new FileSystemWatcher ();
98 if (Directory.Exists (filenames [n])) {
99 watcher.Path = filenames [n];
101 string parentPath = Path.GetDirectoryName (filenames [n]);
102 if (parentPath != null && Directory.Exists (parentPath)) {
103 watcher.Path = parentPath;
104 watcher.Filter = Path.GetFileName (filenames [n]);
108 watcher.NotifyFilter |= NotifyFilters.Size;
109 watcher.Created += new FileSystemEventHandler (OnChanged);
110 watcher.Changed += new FileSystemEventHandler (OnChanged);
111 watcher.Deleted += new FileSystemEventHandler (OnChanged);
112 watcher.Renamed += new RenamedEventHandler (OnChanged);
113 watcher.EnableRaisingEvents = true;
114 watchers [n] = watcher;
117 this.cachekeys = cachekeys;
118 this.dependency = dependency;
119 if (dependency != null)
120 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
126 public virtual string GetUniqueID ()
128 StringBuilder sb = new StringBuilder ();
130 if (watchers != null)
131 foreach (FileSystemWatcher fsw in watchers)
132 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
133 sb.AppendFormat ("_{0}", fsw.Path);
136 if (cachekeys != null)
137 foreach (string key in cachekeys)
138 sb.AppendFormat ("_{0}", key);
139 return sb.ToString ();
142 void OnChanged (object sender, FileSystemEventArgs args)
144 OnDependencyChanged (sender, args);
149 if (DateTime.Now < start)
152 utcLastModified = DateTime.UtcNow;
156 cache.CheckDependencies ();
161 void DisposeWatchers ()
164 if (watchers != null) {
165 foreach (FileSystemWatcher w in watchers)
173 public void Dispose ()
175 DependencyDispose ();
178 internal virtual void DependencyDisposeInternal ()
182 protected virtual void DependencyDispose ()
184 DependencyDisposeInternal ();
186 if (dependency != null) {
187 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
188 dependency.Dispose ();
194 internal void SetCache (Cache c)
200 protected internal void FinishInit ()
202 utcLastModified = DateTime.UtcNow;
205 internal bool IsUsed {
209 internal DateTime Start {
210 get { return start; }
211 set { start = value; }
214 public DateTime UtcLastModified {
216 return utcLastModified;
220 protected void SetUtcLastModified (DateTime utcLastModified)
222 this.utcLastModified = utcLastModified;
225 public bool HasChanged {
230 if (DateTime.Now < start)
233 if (cache != null && cachekeys != null) {
234 foreach (string key in cachekeys) {
235 if (cache.GetKeyLastChange (key) > start) {
248 void OnChildDependencyChanged (object o, EventArgs e)
251 OnDependencyChanged (o, e);
254 void OnDependencyChanged (object sender, EventArgs e)
259 EventHandler eh = events [dependencyChangedEvent] as EventHandler;
264 protected void NotifyDependencyChanged (object sender, EventArgs e)
266 OnDependencyChanged (sender, e);