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;
34 namespace System.Web.Caching
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public class CacheDependency: IDisposable {
42 // CAS - no InheritanceDemand here as the class is sealed
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 public sealed class CacheDependency: IDisposable {
47 CacheDependency dependency;
51 FileSystemWatcher[] watchers;
54 object locker = new object ();
56 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
60 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
64 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
68 public CacheDependency (string [] filenames, DateTime start)
69 : this (filenames, null, null, start)
73 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
77 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
81 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
85 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
88 if (filenames != null) {
89 watchers = new FileSystemWatcher [filenames.Length];
90 for (int n=0; n<filenames.Length; n++) {
91 FileSystemWatcher watcher = new FileSystemWatcher ();
92 if (Directory.Exists (filenames [n])) {
93 watcher.Path = filenames [n];
95 string parentPath = Path.GetDirectoryName (filenames [n]);
96 if (parentPath != null && Directory.Exists (parentPath)) {
97 watcher.Path = parentPath;
98 watcher.Filter = Path.GetFileName (filenames [n]);
102 watcher.Created += new FileSystemEventHandler (OnChanged);
103 watcher.Changed += new FileSystemEventHandler (OnChanged);
104 watcher.Deleted += new FileSystemEventHandler (OnChanged);
105 watcher.Renamed += new RenamedEventHandler (OnChanged);
106 watcher.EnableRaisingEvents = true;
107 watchers [n] = watcher;
111 this.cachekeys = cachekeys;
112 this.dependency = dependency;
113 if (dependency != null)
114 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
118 void OnChanged (object sender, FileSystemEventArgs args)
120 if (DateTime.Now < start)
126 cache.CheckExpiration ();
130 void DisposeWatchers ()
134 void DisposeWatchers ()
137 if (watchers != null) {
138 foreach (FileSystemWatcher w in watchers)
146 public void Dispose ()
149 if (dependency != null)
150 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
154 internal void SetCache (Cache c)
159 public bool HasChanged {
164 if (DateTime.Now < start)
168 if (cachekeys != null) {
169 foreach (string key in cachekeys) {
170 if (cache.GetKeyLastChange (key) > start) {
184 void OnChildDependencyChanged (object o, EventArgs a)
187 OnDependencyChanged ();
190 void OnDependencyChanged ()
192 if (DependencyChanged != null)
193 DependencyChanged (this, null);
196 internal event EventHandler DependencyChanged;