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;
37 namespace System.Web.Caching
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class CacheDependency: IDisposable {
45 // CAS - no InheritanceDemand here as the class is sealed
46 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public sealed class CacheDependency: IDisposable {
49 static readonly object dependencyChangedEvent = new object ();
51 CacheDependency dependency;
54 FileSystemWatcher[] watchers;
58 DateTime utcLastModified;
60 object locker = new object ();
61 EventHandlerList events = new EventHandlerList ();
63 internal event EventHandler DependencyChanged {
64 add { events.AddHandler (dependencyChangedEvent, value); }
65 remove { events.RemoveHandler (dependencyChangedEvent, value); }
69 public CacheDependency (): this (null, null, null, DateTime.Now)
74 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
78 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
82 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
86 public CacheDependency (string [] filenames, DateTime start)
87 : this (filenames, null, null, start)
91 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
95 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
99 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
103 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
105 if (filenames != null) {
106 watchers = new FileSystemWatcher [filenames.Length];
107 for (int n=0; n<filenames.Length; n++) {
108 FileSystemWatcher watcher = new FileSystemWatcher ();
109 if (Directory.Exists (filenames [n])) {
110 watcher.Path = filenames [n];
112 string parentPath = Path.GetDirectoryName (filenames [n]);
113 if (parentPath != null && Directory.Exists (parentPath)) {
114 watcher.Path = parentPath;
115 watcher.Filter = Path.GetFileName (filenames [n]);
119 watcher.NotifyFilter |= NotifyFilters.Size;
120 watcher.Created += new FileSystemEventHandler (OnChanged);
121 watcher.Changed += new FileSystemEventHandler (OnChanged);
122 watcher.Deleted += new FileSystemEventHandler (OnChanged);
123 watcher.Renamed += new RenamedEventHandler (OnChanged);
124 watcher.EnableRaisingEvents = true;
125 watchers [n] = watcher;
128 this.cachekeys = cachekeys;
129 this.dependency = dependency;
130 if (dependency != null)
131 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
140 public virtual string GetUniqueID ()
142 StringBuilder 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.AppendFormat ("_{0}", fsw.Path);
150 if (cachekeys != null)
151 foreach (string key in cachekeys)
152 sb.AppendFormat ("_{0}", key);
153 return sb.ToString ();
157 void OnChanged (object sender, FileSystemEventArgs args)
159 OnDependencyChanged (sender, args);
164 if (DateTime.Now < start)
168 utcLastModified = DateTime.UtcNow;
173 cache.CheckDependencies ();
178 void DisposeWatchers ()
181 if (watchers != null) {
182 foreach (FileSystemWatcher w in watchers)
190 public void Dispose ()
192 DependencyDispose ();
196 internal virtual void DependencyDisposeInternal ()
204 void DependencyDispose ()
207 DependencyDisposeInternal ();
210 if (dependency != null)
211 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
215 internal void SetCache (Cache c)
224 protected internal void FinishInit ()
226 utcLastModified = DateTime.UtcNow;
229 internal bool IsUsed {
233 internal DateTime Start {
234 get { return start; }
235 set { start = value; }
238 public DateTime UtcLastModified {
240 return utcLastModified;
244 protected void SetUtcLastModified (DateTime utcLastModified)
246 this.utcLastModified = utcLastModified;
250 public bool HasChanged {
255 if (DateTime.Now < start)
258 if (cache != null && cachekeys != null) {
259 foreach (string key in cachekeys) {
260 if (cache.GetKeyLastChange (key) > start) {
273 void OnChildDependencyChanged (object o, EventArgs e)
276 OnDependencyChanged (o, e);
279 void OnDependencyChanged (object sender, EventArgs e)
284 EventHandler eh = events [dependencyChangedEvent] as EventHandler;
294 void NotifyDependencyChanged (object sender, EventArgs e)
296 OnDependencyChanged (sender, e);