2 // System.Web.Caching.CacheDependency
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System.Web.Caching
37 internal class CacheDependencyChangedArgs : EventArgs
41 public CacheDependencyChangedArgs (string key)
51 internal delegate void CacheDependencyChangedHandler (object sender, CacheDependencyChangedArgs args);
53 public sealed class CacheDependency : IDisposable
55 static string [] noStrings = new string [0];
56 static CacheDependency noDependency = new CacheDependency ();
60 CacheEntry [] entries;
61 CacheItemRemovedCallback removedDelegate;
62 FileSystemWatcher [] watchers;
64 private CacheDependency ()
68 public CacheDependency (string filename)
69 : this (filename, DateTime.MaxValue)
73 public CacheDependency (string filename, DateTime start)
74 : this (new string [] {filename}, null, null, start)
78 public CacheDependency (string [] filenames)
79 : this (filenames, null, null, DateTime.MaxValue)
83 public CacheDependency (string [] filenames, DateTime start)
84 : this (filenames, null, null, start)
88 public CacheDependency (string [] filenames, string [] cachekeys)
89 : this (filenames, cachekeys, null, DateTime.MaxValue)
93 public CacheDependency (string [] filenames, string [] cachekeys, DateTime start)
94 : this (filenames, cachekeys, null, start)
98 public CacheDependency (string [] filenames, string [] cachekeys, CacheDependency dependency)
99 : this (filenames, cachekeys, dependency, DateTime.MaxValue)
103 public CacheDependency (string [] filenames,
105 CacheDependency dependency,
108 Cache cache = HttpRuntime.Cache;
111 if (filenames == null)
112 filenames = noStrings;
114 foreach (string file in filenames) {
116 throw new ArgumentNullException ("filenames");
119 if (cachekeys == null)
120 cachekeys = noStrings;
122 int missing_keys = 0;
123 foreach (string ck in cachekeys) {
125 throw new ArgumentNullException ("cachekeys");
126 if (cache.GetEntry (ck) == null)
130 if (dependency == null)
131 dependency = noDependency;
134 this.changed = dependency.changed;
138 int nentries = cachekeys.Length + ((dependency.entries == null) ? 0 :
139 dependency.entries.Length) - missing_keys;
142 this.removedDelegate = new CacheItemRemovedCallback (CacheItemRemoved);
143 this.entries = new CacheEntry [nentries];
146 if (dependency.entries != null) {
147 foreach (CacheEntry entry in dependency.entries) {
148 entry._onRemoved += removedDelegate;
149 entries [i++] = entry;
153 for (int c=0; c<cachekeys.Length; c++) {
154 CacheEntry entry = cache.GetEntry (cachekeys [c]);
157 entry._onRemoved += removedDelegate;
158 entries [i++] = entry;
162 if (filenames.Length > 0) {
163 watchers = new FileSystemWatcher [filenames.Length];
164 for (int i=0; i<filenames.Length; i++)
165 watchers [i] = CreateWatcher (filenames [i]);
169 private FileSystemWatcher CreateWatcher (string file)
171 FileSystemWatcher watcher = new FileSystemWatcher ();
173 watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
174 watcher.Filter = Path.GetFileName (file);
176 watcher.Changed += new FileSystemEventHandler (OnFileChanged);
177 watcher.Created += new FileSystemEventHandler (OnFileChanged);
178 watcher.Deleted += new FileSystemEventHandler (OnFileChanged);
180 watcher.EnableRaisingEvents = true;
185 private void OnFileChanged (object sender, FileSystemEventArgs e)
187 OnChanged (sender, e);
190 void CacheItemRemoved (string key, object value, CacheItemRemovedReason reason)
192 OnChanged (this, EventArgs.Empty);
195 void OnChanged (object sender, EventArgs args)
197 if (changed || disposed)
202 Changed (this, new CacheDependencyChangedArgs (null));
205 public void Dispose ()
207 for (int i=0; i<watchers.Length; i++)
208 watchers [i].Dispose ();
211 public bool HasChanged
213 get { return changed; }
216 internal CacheEntry [] GetCacheEntries ()
221 internal event CacheDependencyChangedHandler Changed;