2003-10-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Fri, 3 Oct 2003 20:13:43 +0000 (20:13 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Fri, 3 Oct 2003 20:13:43 +0000 (20:13 -0000)
* ConfigurationSettings.cs: really make the cache work.

svn path=/trunk/mcs/; revision=18568

mcs/class/System/System.Configuration/ChangeLog
mcs/class/System/System.Configuration/ConfigurationSettings.cs

index c4637be738489405255d490dd0c5c28726cbe745..27112c56fe597b691591659aaf17af161411cc34 100644 (file)
@@ -1,3 +1,7 @@
+2003-10-03  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * ConfigurationSettings.cs: really make the cache work.
+
 2003-10-03  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * ConfigurationSettings.cs: pass the file name we're reading as the
index bb87217bd38871d5e81eda7fbc6d25a785774d4e..8ca2a81dfd0e329e99afd519127f9a01f6d08839 100644 (file)
@@ -55,8 +55,8 @@ namespace System.Configuration
        //
        class DefaultConfig : IConfigurationSystem
        {
-               static string creatingInstance = "137213797382-asad";
-               static string buildingData = "1797382-ladgasjkdg";
+               static object creatingInstance = new object ();
+               static object buildingData = new object ();
                static DefaultConfig instance;
                ConfigurationData config;
 
@@ -137,59 +137,47 @@ namespace System.Configuration
         //
         class FileWatcherCache
         {
-                Hashtable _cacheTable;
-                FileInfo _lastInfo;
-                string _filename;
+                Hashtable cacheTable;
+               DateTime lastWriteTime;
+                string filename;
+               static TimeSpan seconds = new TimeSpan (0, 0, 2);
 
                 public FileWatcherCache (string filename)
                 {
-                        _cacheTable = Hashtable.Synchronized (new Hashtable());
-                        _lastInfo = new FileInfo (filename);
-                        _filename = filename;
+                        cacheTable = Hashtable.Synchronized (new Hashtable ());
+                        lastWriteTime = new FileInfo (filename).LastWriteTime;
+                        this.filename = filename;
                 }
 
-                private bool HasFileChanged()
+                void CheckFileChange ()
                 {
-                        FileInfo currentInfo = new FileInfo (_filename);
+                       FileInfo info = new FileInfo (filename);
 
-                        if (currentInfo.Exists == false)
-                                return (true);
-
-                        if (_lastInfo.LastWriteTime != currentInfo.LastWriteTime)
-                                return (true);
-
-                        if (_lastInfo.CreationTime != currentInfo.CreationTime)
-                                return (true);
-
-                        if (_lastInfo.Length != currentInfo.Length)
-                                return (true);
-
-                        return (false);
-                }
-
-                private void CheckFileChange()
-                {
-                        if (HasFileChanged() == true)
-                        {
-                                _lastInfo = new FileInfo (_filename);
-
-                                _cacheTable.Clear();
-                        }
-                }
-
-                public void Set (string key, object value)
-                {
-                        CheckFileChange();
+                       if (!info.Exists) {
+                               lastWriteTime = DateTime.MinValue;
+                               cacheTable.Clear ();
+                               return;
+                       }
 
-                        _cacheTable[key] = value;
+                       DateTime writeTime = info.LastWriteTime;
+                       TimeSpan ts = (info.LastWriteTime - lastWriteTime);
+                       if (ts >= seconds) {
+                               lastWriteTime = writeTime;
+                               cacheTable.Clear ();
+                       }
                 }
 
-                public object Get (string key)
-                {
-                        CheckFileChange();
+               public object this [string key] {
+                       get {
+                               CheckFileChange ();
+                               return cacheTable [key];
+                       }
 
-                        return (_cacheTable[key]);
-                }
+                       set {
+                               CheckFileChange();
+                               cacheTable [key] = value;
+                       }
+               }
         }
 
        class ConfigurationData
@@ -202,23 +190,17 @@ namespace System.Configuration
                 object emptyMark = new object ();
                 FileWatcherCache fileCache = null;
 
-                private FileWatcherCache FileCache
-                {
-                        get
-                        {
-                                if (fileCache == null)
-                                {
-                                        if (fileName != null)
-                                        {
+                private FileWatcherCache FileCache {
+                        get {
+                                if (fileCache == null) {
+                                        if (fileName != null) {
                                                 fileCache = new FileWatcherCache (fileName);
-                                        }
-                                        else
-                                        {
+                                        } else {
                                                 fileCache = parent.FileCache;
                                         }
                                 }
 
-                                return (fileCache);
+                                return fileCache;
                         }
                 }
 
@@ -353,26 +335,17 @@ namespace System.Configuration
 
                public object GetConfig (string sectionName)
                {
-                        object config;
-
-                        // check to see if the handler is in the cache
-                        config = this.FileCache.Get (sectionName);
+                        object config = this.FileCache [sectionName];
 
                         if (config == emptyMark)
-                                return (null);
-                        else if (config != null)
-                                return (config);
-                        else
-                        {
-                                config = GetConfigInternal (sectionName);
-
-                                if (config == null)
-                                        this.FileCache.Set (sectionName, emptyMark);
-                                else
-                                        this.FileCache.Set (sectionName, config);
-
-                                return (config);
-                        }
+                                return null;
+
+                        if (config != null)
+                                return config;
+
+                       config = GetConfigInternal (sectionName);
+                       this.FileCache [sectionName] = (config == null) ? emptyMark : config;
+                       return config;
                 }
 
                private object LookForFactory (string key)