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.
30 using System.Collections;
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 {
50 CacheDependency dependency;
53 FileSystemWatcher[] watchers;
57 DateTime utcLastModified;
59 object locker = new object ();
62 public CacheDependency (): this (null, null, null, DateTime.Now)
67 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
71 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
75 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
79 public CacheDependency (string [] filenames, DateTime start)
80 : this (filenames, null, null, start)
84 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
88 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
92 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
96 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
98 if (filenames != null) {
99 watchers = new FileSystemWatcher [filenames.Length];
100 for (int n=0; n<filenames.Length; n++) {
101 FileSystemWatcher watcher = new FileSystemWatcher ();
102 if (Directory.Exists (filenames [n])) {
103 watcher.Path = filenames [n];
105 string parentPath = Path.GetDirectoryName (filenames [n]);
106 if (parentPath != null && Directory.Exists (parentPath)) {
107 watcher.Path = parentPath;
108 watcher.Filter = Path.GetFileName (filenames [n]);
112 watcher.NotifyFilter |= NotifyFilters.Size;
113 watcher.Created += new FileSystemEventHandler (OnChanged);
114 watcher.Changed += new FileSystemEventHandler (OnChanged);
115 watcher.Deleted += new FileSystemEventHandler (OnChanged);
116 watcher.Renamed += new RenamedEventHandler (OnChanged);
117 watcher.EnableRaisingEvents = true;
118 watchers [n] = watcher;
121 this.cachekeys = cachekeys;
122 this.dependency = dependency;
123 if (dependency != null)
124 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
133 public virtual string GetUniqueID ()
135 StringBuilder sb = new StringBuilder ();
137 if (watchers != null)
138 foreach (FileSystemWatcher fsw in watchers)
139 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
140 sb.AppendFormat ("_{0}", fsw.Path);
143 if (cachekeys != null)
144 foreach (string key in cachekeys)
145 sb.AppendFormat ("_{0}", key);
146 return sb.ToString ();
150 void OnChanged (object sender, FileSystemEventArgs args)
152 OnDependencyChanged (sender, args);
157 if (DateTime.Now < start)
161 utcLastModified = DateTime.UtcNow;
166 cache.CheckExpiration ();
171 void DisposeWatchers ()
174 if (watchers != null) {
175 foreach (FileSystemWatcher w in watchers)
183 public void Dispose ()
185 DependencyDispose ();
189 internal virtual void DependencyDisposeInternal ()
197 void DependencyDispose ()
200 DependencyDisposeInternal ();
203 if (dependency != null)
204 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
208 internal void SetCache (Cache c)
217 protected internal void FinishInit ()
219 utcLastModified = DateTime.UtcNow;
222 internal bool IsUsed {
226 internal DateTime Start {
227 get { return start; }
228 set { start = value; }
231 public DateTime UtcLastModified {
233 return utcLastModified;
237 protected void SetUtcLastModified (DateTime utcLastModified)
239 this.utcLastModified = utcLastModified;
243 public bool HasChanged {
248 if (DateTime.Now < start)
251 if (cache != null && cachekeys != null) {
252 foreach (string key in cachekeys) {
253 if (cache.GetKeyLastChange (key) > start) {
266 void OnChildDependencyChanged (object o, EventArgs e)
269 OnDependencyChanged (o, e);
272 void OnDependencyChanged (object sender, EventArgs e)
277 if (DependencyChanged == null)
280 foreach (EventHandler eh in DependencyChanged.GetInvocationList ())
289 void NotifyDependencyChanged (object sender, EventArgs e)
291 OnDependencyChanged (sender, e);
294 internal event EventHandler DependencyChanged;