diff --git a/MinecraftClient/Cache/SessionCache.cs b/MinecraftClient/Cache/SessionCache.cs index 9064d232..a943958b 100644 --- a/MinecraftClient/Cache/SessionCache.cs +++ b/MinecraftClient/Cache/SessionCache.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; +using System.Timers; namespace MinecraftClient.Cache { @@ -16,6 +17,8 @@ namespace MinecraftClient.Cache const string filename = "cache.bin"; private static Dictionary sessions = new Dictionary(); private static FileSystemWatcher cachemonitor = new FileSystemWatcher(); + private static Timer updatetimer = new Timer(100); + private static List> pendingadds = new List>(); private static BinaryFormatter formatter = new BinaryFormatter(); @@ -47,7 +50,9 @@ namespace MinecraftClient.Cache sessions.Add(login, session); } - if (Settings.CacheType == CacheType.DISK) + if (Settings.CacheType == CacheType.DISK && updatetimer.Enabled == true) { + pendingadds.Add(new KeyValuePair(login, session)); + }else if (Settings.CacheType == CacheType.DISK) { SaveToDisk(); } @@ -78,18 +83,38 @@ namespace MinecraftClient.Cache cachemonitor.Changed += new FileSystemEventHandler(OnChanged); cachemonitor.EnableRaisingEvents = true; + updatetimer.Elapsed += HandlePending; + return LoadFromDisk(); } /// /// Reloads cache on external cache file change. /// - /// Sender + /// Sender /// Event data - private static void OnChanged(object source, FileSystemEventArgs e) + private static void OnChanged(object sender, FileSystemEventArgs e) + { + updatetimer.Stop(); + updatetimer.Start(); + } + + /// + /// Called after timer elapsed. Reads disk cache and adds new/modified sessions back. + /// + /// Sender + /// Event data + + private static void HandlePending(object sender, ElapsedEventArgs e) { LoadFromDisk(); + + foreach(KeyValuePair pending in pendingadds.ToArray()) + { + Store(pending.Key, pending.Value); + pendingadds.Remove(pending); + } } /// diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index c8d15486..402046e4 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -88,7 +88,7 @@ namespace MinecraftClient //Load cached sessions from disk if necessary if (Settings.CacheType == Cache.CacheType.DISK) { - Console.WriteLine(Cache.SessionCache.InitializeDiskCache() ? "Cached sessions loaded." : "ยง8Cached sessions could not be loaded from disk"); + Console.WriteLine(Cache.SessionCache.InitializeDiskCache() ? "Cached sessions loaded." : "Cached sessions could not be loaded from disk"); } //Asking the user to type in missing data such as Username and Password