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); }
64 CacheDependency (): this (null, null, null, DateTime.Now)
68 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
72 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
76 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
80 public CacheDependency (string [] filenames, DateTime start)
81 : this (filenames, null, null, start)
85 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
89 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
93 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
97 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
99 int flen = filenames != null ? filenames.Length : 0;
102 watchers = new FileSystemWatcher [flen];
105 for (int n = 0; n < flen; n++) {
106 filename = filenames [n];
107 if (String.IsNullOrEmpty (filename))
110 FileSystemWatcher watcher = new FileSystemWatcher ();
111 if (Directory.Exists (filename))
112 watcher.Path = filename;
114 string parentPath = Path.GetDirectoryName (filename);
115 if (parentPath != null && Directory.Exists (parentPath)) {
116 watcher.Path = parentPath;
117 watcher.Filter = Path.GetFileName (filename);
121 watcher.NotifyFilter |= NotifyFilters.Size;
122 watcher.Created += new FileSystemEventHandler (OnChanged);
123 watcher.Changed += new FileSystemEventHandler (OnChanged);
124 watcher.Deleted += new FileSystemEventHandler (OnChanged);
125 watcher.Renamed += new RenamedEventHandler (OnChanged);
126 watcher.EnableRaisingEvents = true;
127 watchers [n] = watcher;
130 this.cachekeys = cachekeys;
131 this.dependency = dependency;
132 if (dependency != null)
133 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
139 public virtual string GetUniqueID ()
141 var sb = new StringBuilder ();
144 if (watchers != null)
145 foreach (FileSystemWatcher fsw in watchers)
146 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
147 sb.Append ("_" + fsw.Path);
150 if (cachekeys != null)
151 foreach (string key in cachekeys)
152 sb.AppendFormat ("_" + key);
153 return sb.ToString ();
156 void OnChanged (object sender, FileSystemEventArgs args)
158 OnDependencyChanged (sender, args);
163 DateTime now = DateTime.Now;
168 utcLastModified = now.ToUniversalTime ();
172 cache.CheckDependencies ();
177 void DisposeWatchers ()
180 if (watchers != null) {
181 foreach (FileSystemWatcher w in watchers)
189 public void Dispose ()
191 DependencyDispose ();
194 internal virtual void DependencyDisposeInternal ()
198 protected virtual void DependencyDispose ()
200 DependencyDisposeInternal ();
202 if (dependency != null) {
203 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
204 dependency.Dispose ();
210 internal void SetCache (Cache c)
216 protected internal void FinishInit ()
218 utcLastModified = DateTime.UtcNow;
221 internal bool IsUsed {
225 internal DateTime Start {
226 get { return start; }
227 set { start = value; }
230 public DateTime UtcLastModified {
232 return utcLastModified;
236 protected void SetUtcLastModified (DateTime utcLastModified)
238 this.utcLastModified = utcLastModified;
241 public bool HasChanged {
246 if (DateTime.Now < start)
249 if (cache != null && cachekeys != null) {
250 foreach (string key in cachekeys) {
251 if (cache.GetKeyLastChange (key) > start) {
264 void OnChildDependencyChanged (object o, EventArgs e)
267 OnDependencyChanged (o, e);
270 void OnDependencyChanged (object sender, EventArgs e)
275 EventHandler eh = events [dependencyChangedEvent] as EventHandler;
280 protected void NotifyDependencyChanged (object sender, EventArgs e)
282 OnDependencyChanged (sender, e);