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;
35 namespace System.Web.Caching
39 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public class CacheDependency: IDisposable {
43 // CAS - no InheritanceDemand here as the class is sealed
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public sealed class CacheDependency: IDisposable {
48 CacheDependency dependency;
52 FileSystemWatcher[] watchers;
55 object locker = new object ();
59 protected CacheDependency ()
64 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
68 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
72 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
76 public CacheDependency (string [] filenames, DateTime start)
77 : this (filenames, null, null, start)
81 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
85 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
89 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
93 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
96 StringBuilder sb = new StringBuilder ();
100 if (filenames != null) {
101 watchers = new FileSystemWatcher [filenames.Length];
102 for (int n=0; n<filenames.Length; n++) {
103 FileSystemWatcher watcher = new FileSystemWatcher ();
105 sb.Append (filenames [n]);
107 if (Directory.Exists (filenames [n])) {
108 watcher.Path = filenames [n];
110 string parentPath = Path.GetDirectoryName (filenames [n]);
111 if (parentPath != null && Directory.Exists (parentPath)) {
112 watcher.Path = parentPath;
113 watcher.Filter = Path.GetFileName (filenames [n]);
117 watcher.Created += new FileSystemEventHandler (OnChanged);
118 watcher.Changed += new FileSystemEventHandler (OnChanged);
119 watcher.Deleted += new FileSystemEventHandler (OnChanged);
120 watcher.Renamed += new RenamedEventHandler (OnChanged);
121 watcher.EnableRaisingEvents = true;
122 watchers [n] = watcher;
126 this.cachekeys = cachekeys;
128 if (cachekeys != null) {
129 foreach (string s in cachekeys)
132 if (sb.Length > 5) // 5 is the length of "@UID@"
133 unique_id = sb.ToString ();
136 this.dependency = dependency;
137 if (dependency != null)
138 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
142 void OnChanged (object sender, FileSystemEventArgs args)
144 if (DateTime.Now < start)
150 cache.CheckExpiration ();
153 void DisposeWatchers ()
156 if (watchers != null) {
157 foreach (FileSystemWatcher w in watchers)
165 void DisposeWatchers ()
169 public void Dispose ()
172 if (dependency != null)
173 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
177 internal void SetCache (Cache c)
183 public virtual string GetUniqueID ()
188 public virtual bool HasChanged {
190 public bool HasChanged {
196 if (DateTime.Now < start)
199 if (cache != null && cachekeys != null) {
200 foreach (string key in cachekeys) {
201 if (cache.GetKeyLastChange (key) > start) {
214 void OnChildDependencyChanged (object o, EventArgs a)
217 OnDependencyChanged ();
220 internal void OnDependencyChanged ()
222 if (DependencyChanged != null)
223 DependencyChanged (this, null);
226 internal event EventHandler DependencyChanged;