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;
54 FileSystemWatcher[] watchers;
60 object locker = new object ();
63 public 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)
100 if (filenames != null) {
101 watchers = new FileSystemWatcher [filenames.Length];
102 for (int n=0; n<filenames.Length; n++) {
103 FileSystemWatcher watcher = new FileSystemWatcher ();
104 if (Directory.Exists (filenames [n])) {
105 watcher.Path = filenames [n];
107 string parentPath = Path.GetDirectoryName (filenames [n]);
108 if (parentPath != null && Directory.Exists (parentPath)) {
109 watcher.Path = parentPath;
110 watcher.Filter = Path.GetFileName (filenames [n]);
114 watcher.Created += new FileSystemEventHandler (OnChanged);
115 watcher.Changed += new FileSystemEventHandler (OnChanged);
116 watcher.Deleted += new FileSystemEventHandler (OnChanged);
117 watcher.Renamed += new RenamedEventHandler (OnChanged);
118 watcher.EnableRaisingEvents = true;
119 watchers [n] = watcher;
123 this.cachekeys = cachekeys;
124 this.dependency = dependency;
125 if (dependency != null)
126 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
131 public virtual string GetUniqueID ()
133 StringBuilder sb = new StringBuilder ();
136 if (watchers != null)
137 foreach (FileSystemWatcher fsw in watchers)
138 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
139 sb.AppendFormat ("_{0}", fsw.Path);
143 if (cachekeys != null)
144 foreach (string key in cachekeys)
145 sb.AppendFormat ("_{0}", key);
146 return sb.ToString ();
151 void OnChanged (object sender, FileSystemEventArgs args)
153 if (DateTime.Now < start)
159 cache.CheckExpiration ();
162 void DisposeWatchers ()
165 if (watchers != null) {
166 foreach (FileSystemWatcher w in watchers)
174 void DisposeWatchers ()
178 public void Dispose ()
181 if (dependency != null)
182 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
186 internal void SetCache (Cache c)
195 internal bool IsUsed {
199 internal DateTime Start {
200 get { return start; }
201 set { start = value; }
205 public bool HasChanged {
210 if (DateTime.Now < start)
213 if (cache != null && cachekeys != null) {
214 foreach (string key in cachekeys) {
215 if (cache.GetKeyLastChange (key) > start) {
228 void OnChildDependencyChanged (object o, EventArgs a)
231 OnDependencyChanged ();
234 void OnDependencyChanged ()
236 if (DependencyChanged != null)
237 DependencyChanged (this, null);
241 internal void SignalDependencyChanged ()
243 OnDependencyChanged ();
246 internal event EventHandler DependencyChanged;