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); }
60 CacheDependency (): this (null, null, null, DateTime.Now)
64 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
68 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
72 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
76 public CacheDependency (string [] filenames, DateTime start)
77 : this (filenames, null, null, start)
81 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
85 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
89 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
93 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
95 int flen = filenames != null ? filenames.Length : 0;
98 watchers = new FileSystemWatcher [flen];
101 for (int n = 0; n < flen; n++) {
102 filename = filenames [n];
103 if (String.IsNullOrEmpty (filename))
106 FileSystemWatcher watcher = new FileSystemWatcher ();
107 if (Directory.Exists (filename))
108 watcher.Path = filename;
110 string parentPath = Path.GetDirectoryName (filename);
111 if (parentPath != null && Directory.Exists (parentPath)) {
112 watcher.Path = parentPath;
113 watcher.Filter = Path.GetFileName (filename);
117 watcher.NotifyFilter |= NotifyFilters.Size;
118 watcher.Created += new FileSystemEventHandler (OnChanged);
119 watcher.Changed += new FileSystemEventHandler (OnChanged);
120 watcher.Deleted += new FileSystemEventHandler (OnChanged);
121 watcher.Renamed += new RenamedEventHandler (OnChanged);
122 watcher.EnableRaisingEvents = true;
123 watchers [n] = watcher;
126 this.cachekeys = cachekeys;
127 this.dependency = dependency;
128 if (dependency != null)
129 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
135 public virtual string GetUniqueID ()
137 var sb = new StringBuilder ();
140 if (watchers != null)
141 foreach (FileSystemWatcher fsw in watchers)
142 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
143 sb.Append ("_" + fsw.Path);
146 if (cachekeys != null)
147 foreach (string key in cachekeys)
148 sb.AppendFormat ("_" + key);
149 return sb.ToString ();
152 void OnChanged (object sender, FileSystemEventArgs args)
154 OnDependencyChanged (sender, args);
159 DateTime now = DateTime.Now;
164 utcLastModified = now.ToUniversalTime ();
168 cache.CheckDependencies ();
173 void DisposeWatchers ()
176 if (watchers != null) {
177 foreach (FileSystemWatcher w in watchers)
185 public void Dispose ()
187 DependencyDispose ();
190 internal virtual void DependencyDisposeInternal ()
194 protected virtual void DependencyDispose ()
196 DependencyDisposeInternal ();
198 if (dependency != null) {
199 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
200 dependency.Dispose ();
206 internal void SetCache (Cache c)
212 protected internal void FinishInit ()
214 utcLastModified = DateTime.UtcNow;
217 internal bool IsUsed {
221 internal DateTime Start {
222 get { return start; }
223 set { start = value; }
226 public DateTime UtcLastModified {
228 return utcLastModified;
232 protected void SetUtcLastModified (DateTime utcLastModified)
234 this.utcLastModified = utcLastModified;
237 public bool HasChanged {
242 if (DateTime.Now < start)
245 if (cache != null && cachekeys != null) {
246 foreach (string key in cachekeys) {
247 if (cache.GetKeyLastChange (key) > start) {
260 void OnChildDependencyChanged (object o, EventArgs e)
263 OnDependencyChanged (o, e);
266 void OnDependencyChanged (object sender, EventArgs e)
271 EventHandler eh = events [dependencyChangedEvent] as EventHandler;
276 protected void NotifyDependencyChanged (object sender, EventArgs e)
278 OnDependencyChanged (sender, e);