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;
58 object locker = new object ();
61 public CacheDependency (): this (null, null, null, DateTime.Now)
66 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
70 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
74 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
78 public CacheDependency (string [] filenames, DateTime start)
79 : this (filenames, null, null, start)
83 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
87 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
91 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
95 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
97 if (filenames != null) {
98 watchers = new FileSystemWatcher [filenames.Length];
99 for (int n=0; n<filenames.Length; n++) {
100 FileSystemWatcher watcher = new FileSystemWatcher ();
101 if (Directory.Exists (filenames [n])) {
102 watcher.Path = filenames [n];
104 string parentPath = Path.GetDirectoryName (filenames [n]);
105 if (parentPath != null && Directory.Exists (parentPath)) {
106 watcher.Path = parentPath;
107 watcher.Filter = Path.GetFileName (filenames [n]);
111 watcher.Created += new FileSystemEventHandler (OnChanged);
112 watcher.Changed += new FileSystemEventHandler (OnChanged);
113 watcher.Deleted += new FileSystemEventHandler (OnChanged);
114 watcher.Renamed += new RenamedEventHandler (OnChanged);
115 watcher.EnableRaisingEvents = true;
116 watchers [n] = watcher;
119 this.cachekeys = cachekeys;
120 this.dependency = dependency;
121 if (dependency != null)
122 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
127 public virtual string GetUniqueID ()
129 StringBuilder sb = new StringBuilder ();
131 if (watchers != null)
132 foreach (FileSystemWatcher fsw in watchers)
133 if (fsw != null && fsw.Path != null && fsw.Path.Length != 0)
134 sb.AppendFormat ("_{0}", fsw.Path);
137 if (cachekeys != null)
138 foreach (string key in cachekeys)
139 sb.AppendFormat ("_{0}", key);
140 return sb.ToString ();
144 void OnChanged (object sender, FileSystemEventArgs args)
146 if (DateTime.Now < start)
152 cache.CheckExpiration ();
155 void DisposeWatchers ()
158 if (watchers != null) {
159 foreach (FileSystemWatcher w in watchers)
166 public void Dispose ()
169 if (dependency != null)
170 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
174 internal void SetCache (Cache c)
183 internal bool IsUsed {
187 internal DateTime Start {
188 get { return start; }
189 set { start = value; }
193 public bool HasChanged {
198 if (DateTime.Now < start)
201 if (cache != null && cachekeys != null) {
202 foreach (string key in cachekeys) {
203 if (cache.GetKeyLastChange (key) > start) {
216 void OnChildDependencyChanged (object o, EventArgs a)
219 OnDependencyChanged ();
222 void OnDependencyChanged ()
224 if (DependencyChanged != null)
225 DependencyChanged (this, null);
229 internal void SignalDependencyChanged ()
231 OnDependencyChanged ();
234 internal event EventHandler DependencyChanged;