added timer to reduce file access collisions and cleaned some text up.

This commit is contained in:
Justin Slauson 2016-03-02 19:08:24 -07:00
parent fec1687cb7
commit 98b3ce7304
2 changed files with 29 additions and 4 deletions

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Timers;
namespace MinecraftClient.Cache namespace MinecraftClient.Cache
{ {
@ -16,6 +17,8 @@ namespace MinecraftClient.Cache
const string filename = "cache.bin"; const string filename = "cache.bin";
private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>(); private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>();
private static FileSystemWatcher cachemonitor = new FileSystemWatcher(); private static FileSystemWatcher cachemonitor = new FileSystemWatcher();
private static Timer updatetimer = new Timer(100);
private static List<KeyValuePair<string, SessionToken>> pendingadds = new List<KeyValuePair<string, SessionToken>>();
private static BinaryFormatter formatter = new BinaryFormatter(); private static BinaryFormatter formatter = new BinaryFormatter();
@ -47,7 +50,9 @@ namespace MinecraftClient.Cache
sessions.Add(login, session); sessions.Add(login, session);
} }
if (Settings.CacheType == CacheType.DISK) if (Settings.CacheType == CacheType.DISK && updatetimer.Enabled == true) {
pendingadds.Add(new KeyValuePair<string, SessionToken>(login, session));
}else if (Settings.CacheType == CacheType.DISK)
{ {
SaveToDisk(); SaveToDisk();
} }
@ -78,18 +83,38 @@ namespace MinecraftClient.Cache
cachemonitor.Changed += new FileSystemEventHandler(OnChanged); cachemonitor.Changed += new FileSystemEventHandler(OnChanged);
cachemonitor.EnableRaisingEvents = true; cachemonitor.EnableRaisingEvents = true;
updatetimer.Elapsed += HandlePending;
return LoadFromDisk(); return LoadFromDisk();
} }
/// <summary> /// <summary>
/// Reloads cache on external cache file change. /// Reloads cache on external cache file change.
/// </summary> /// </summary>
/// <param name="source">Sender</param> /// <param name="sender">Sender</param>
/// <param name="e">Event data</param> /// <param name="e">Event data</param>
private static void OnChanged(object source, FileSystemEventArgs e) private static void OnChanged(object sender, FileSystemEventArgs e)
{
updatetimer.Stop();
updatetimer.Start();
}
/// <summary>
/// Called after timer elapsed. Reads disk cache and adds new/modified sessions back.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event data</param>
private static void HandlePending(object sender, ElapsedEventArgs e)
{ {
LoadFromDisk(); LoadFromDisk();
foreach(KeyValuePair<string, SessionToken> pending in pendingadds.ToArray())
{
Store(pending.Key, pending.Value);
pendingadds.Remove(pending);
}
} }
/// <summary> /// <summary>

View file

@ -88,7 +88,7 @@ namespace MinecraftClient
//Load cached sessions from disk if necessary //Load cached sessions from disk if necessary
if (Settings.CacheType == Cache.CacheType.DISK) 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 //Asking the user to type in missing data such as Username and Password