Skip to content

Commit

Permalink
setting cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TransposonY committed Sep 21, 2020
1 parent 40224f4 commit 2919400
Showing 1 changed file with 68 additions and 33 deletions.
101 changes: 68 additions & 33 deletions GestureSign.Common/Configuration/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ManagedWinapi.Hooks;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
Expand All @@ -12,12 +13,36 @@ namespace GestureSign.Common.Configuration
{
public class AppConfig
{
private static bool _loadFlag = true;
private static Dictionary<string, object> _settingCache = new Dictionary<string, object>(16);
static System.Configuration.Configuration _config;
static Timer Timer;
public static event EventHandler ConfigChanged;

private static ExeConfigurationFileMap ExeMap;

private static System.Configuration.Configuration Config
{
get
{
if (_config == null || _loadFlag)
{
try
{
FileManager.WaitFile(ConfigPath);
_config = ConfigurationManager.OpenMappedExeConfiguration(ExeMap, ConfigurationUserLevel.None);
_settingCache.Clear();
_loadFlag = false;
}
catch (Exception e)
{
Logging.LogException(e);
}
}
return _config;
}
}

public static string ApplicationDataPath { private set; get; }

public static string LocalApplicationDataPath { private set; get; }
Expand All @@ -28,6 +53,8 @@ public class AppConfig

public static string CurrentFolderPath { private set; get; }

#region Setting Parameters

public static System.Drawing.Color VisualFeedbackColor
{
get
Expand Down Expand Up @@ -208,6 +235,8 @@ public static DeviceStates PenGestureButton
}
}

#endregion

static AppConfig()
{
#if uiAccess
Expand All @@ -231,36 +260,20 @@ static AppConfig()
if (!Directory.Exists(ApplicationDataPath))
Directory.CreateDirectory(ApplicationDataPath);

FileManager.WaitFile(ConfigPath);

ExeMap = new ExeConfigurationFileMap
{
ExeConfigFilename = ConfigPath,
RoamingUserConfigFilename = ConfigPath,
};
try
{
_config = ConfigurationManager.OpenMappedExeConfiguration(ExeMap, ConfigurationUserLevel.None);
}
catch (Exception e)
{
Logging.LogException(e);
}
Timer = new Timer(SaveFile, null, Timeout.Infinite, Timeout.Infinite);
}

public static void Reload()
{
try
{
FileManager.WaitFile(ConfigPath);

_config = ConfigurationManager.OpenMappedExeConfiguration(ExeMap, ConfigurationUserLevel.None);
// ConfigurationManager.RefreshSection("appSettings");
if (ConfigChanged != null)
ConfigChanged(new object(), EventArgs.Empty);
}
catch (Exception e) { Logging.LogException(e); }
_loadFlag = true;
_settingCache.Clear();
if (ConfigChanged != null)
ConfigChanged(new object(), EventArgs.Empty);
}


Expand All @@ -275,8 +288,9 @@ private static void SaveFile(object state)
{
FileManager.WaitFile(ConfigPath);
// Save the configuration file.
_config.AppSettings.SectionInformation.ForceSave = true;
_config.Save(ConfigurationSaveMode.Modified);
var config = Config;
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
catch (ConfigurationErrorsException)
{
Expand All @@ -293,7 +307,9 @@ private static void SaveFile(object state)

private static T GetValue<T>(string key, T defaultValue, Func<string, T> converter)
{
var setting = _config.AppSettings.Settings[key];
if (Config == null)
return defaultValue;
var setting = Config.AppSettings.Settings[key];
if (setting != null)
{
try
Expand All @@ -302,26 +318,41 @@ private static T GetValue<T>(string key, T defaultValue, Func<string, T> convert
}
catch
{
_config.AppSettings.Settings.Remove(key);
Config.AppSettings.Settings.Remove(key);
return defaultValue;
}
}
return defaultValue;
}

private static T GetCacheValue<T>(string key, T defaultValue, Func<string, T> converter)
{
object output;
if (_settingCache.TryGetValue(key, out output))
{
return (T)output;
}
else
{
var value = GetValue(key, defaultValue, converter);
_settingCache.Add(key, value);
return value;
}
}

private static int GetValue(string key, int defaultValue)
{
return GetValue(key, defaultValue, s => int.Parse(s));
return GetCacheValue(key, defaultValue, s => int.Parse(s));
}

private static double GetValue(string key, double defaultValue)
{
return GetValue(key, defaultValue, s => double.Parse(s));
return GetCacheValue(key, defaultValue, s => double.Parse(s));
}

private static bool GetValue(string key, bool defaultValue)
{
return GetValue(key, defaultValue, s => bool.Parse(s));
return GetCacheValue(key, defaultValue, s => bool.Parse(s));
}

private static string GetValue(string key, string defaultValue)
Expand All @@ -340,7 +371,7 @@ private static DateTime GetValue(string key, DateTime defaultValue)
}
catch
{
_config.AppSettings.Settings.Remove(key);
Config.AppSettings.Settings.Remove(key);
return defaultValue;
}
}
Expand All @@ -354,11 +385,11 @@ private static System.Drawing.Color GetValue(string key, System.Drawing.Color de
{
try
{
return System.Drawing.ColorTranslator.FromHtml(setting);
return GetCacheValue(key, defaultValue, System.Drawing.ColorTranslator.FromHtml);
}
catch
{
_config.AppSettings.Settings.Remove(key);
Config.AppSettings.Settings.Remove(key);
return defaultValue;
}
}
Expand All @@ -373,13 +404,17 @@ private static System.Drawing.Color GetValue(string key, System.Drawing.Color de

private static void SetValue<T>(string key, T value)
{
if (_config.AppSettings.Settings[key] != null)
if (Config == null)
return;
_settingCache.Clear();

if (Config.AppSettings.Settings[key] != null)
{
_config.AppSettings.Settings[key].Value = value.ToString();
Config.AppSettings.Settings[key].Value = value.ToString();
}
else
{
_config.AppSettings.Settings.Add(key, value.ToString());
Config.AppSettings.Settings.Add(key, value.ToString());
}
Save();
}
Expand Down

0 comments on commit 2919400

Please sign in to comment.