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 ();
59 CacheEntry [] entries;
60 CacheItemRemovedCallback removedDelegate;
61 FileSystemWatcher [] watchers;
63 private CacheDependency ()
67 public CacheDependency (string filename)
68 : this (filename, DateTime.MaxValue)
72 public CacheDependency (string filename, DateTime start)
73 : this (new string [] {filename}, null, null, start)
77 public CacheDependency (string [] filenames)
78 : this (filenames, null, null, DateTime.MaxValue)
82 public CacheDependency (string [] filenames, DateTime start)
83 : this (filenames, null, null, start)
87 public CacheDependency (string [] filenames, string [] cachekeys)
88 : this (filenames, cachekeys, null, DateTime.MaxValue)
92 public CacheDependency (string [] filenames, string [] cachekeys, DateTime start)
93 : this (filenames, cachekeys, null, start)
97 public CacheDependency (string [] filenames, string [] cachekeys, CacheDependency dependency)
98 : this (filenames, cachekeys, dependency, DateTime.MaxValue)
102 public CacheDependency (string [] filenames,
104 CacheDependency dependency,
107 Cache cache = HttpRuntime.Cache;
109 if (filenames == null)
110 filenames = noStrings;
112 foreach (string file in filenames) {
114 throw new ArgumentNullException ("filenames");
117 if (cachekeys == null)
118 cachekeys = noStrings;
120 int missing_keys = 0;
121 foreach (string ck in cachekeys) {
123 throw new ArgumentNullException ("cachekeys");
124 if (cache.GetEntry (ck) == null)
128 if (dependency == null)
129 dependency = noDependency;
132 this.changed = dependency.changed;
136 int nentries = cachekeys.Length + ((dependency.entries == null) ? 0 :
137 dependency.entries.Length) - missing_keys;
140 this.removedDelegate = new CacheItemRemovedCallback (CacheItemRemoved);
141 this.entries = new CacheEntry [nentries];
144 if (dependency.entries != null) {
145 foreach (CacheEntry entry in dependency.entries) {
146 entry._onRemoved += removedDelegate;
147 entries [i++] = entry;
151 for (int c=0; c<cachekeys.Length; c++) {
152 CacheEntry entry = cache.GetEntry (cachekeys [c]);
155 entry._onRemoved += removedDelegate;
156 entries [i++] = entry;
160 if (filenames.Length > 0) {
161 watchers = new FileSystemWatcher [filenames.Length];
162 for (int i=0; i<filenames.Length; i++)
163 watchers [i] = CreateWatcher (filenames [i]);
167 private FileSystemWatcher CreateWatcher (string file)
169 FileSystemWatcher watcher = new FileSystemWatcher ();
171 watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
172 watcher.Filter = Path.GetFileName (file);
174 watcher.Changed += new FileSystemEventHandler (OnFileChanged);
175 watcher.Created += new FileSystemEventHandler (OnFileChanged);
176 watcher.Deleted += new FileSystemEventHandler (OnFileChanged);
178 watcher.EnableRaisingEvents = true;
183 private void OnFileChanged (object sender, FileSystemEventArgs e)
185 OnChanged (sender, e);
188 void CacheItemRemoved (string key, object value, CacheItemRemovedReason reason)
190 OnChanged (this, EventArgs.Empty);
193 void OnChanged (object sender, EventArgs args)
195 if (changed || disposed)
200 Changed (this, new CacheDependencyChangedArgs (null));
203 public void Dispose ()
205 for (int i=0; i<watchers.Length; i++)
206 watchers [i].Dispose ();
209 public bool HasChanged
211 get { return changed; }
214 internal CacheEntry [] GetCacheEntries ()
219 internal event CacheDependencyChangedHandler Changed;