fix: configuration optimization
This commit is contained in:
@@ -34,11 +34,11 @@ namespace Disco.Data.Configuration
|
|||||||
|
|
||||||
private void SetValue<T>(string Key, T Value)
|
private void SetValue<T>(string Key, T Value)
|
||||||
{
|
{
|
||||||
ConfigurationCache.SetValue(Database, this.Scope, Key, Value);
|
ConfigurationCache.Helpers<T>.SetValue(Database, this.Scope, Key, Value);
|
||||||
}
|
}
|
||||||
private T GetValue<T>(string Key, T Default)
|
private T GetValue<T>(string Key, T Default)
|
||||||
{
|
{
|
||||||
return ConfigurationCache.GetValue(Database, this.Scope, Key, Default);
|
return ConfigurationCache.Helpers<T>.GetValue(Database, this.Scope, Key, Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Set<T>(T Value, [CallerMemberName] string Key = null)
|
protected void Set<T>(T Value, [CallerMemberName] string Key = null)
|
||||||
|
|||||||
@@ -202,89 +202,95 @@ namespace Disco.Data.Configuration
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Cache Getters/Setters
|
#region Cache Getters/Setters
|
||||||
internal static T GetValue<T>(DiscoDataContext Database, string Scope, string Key, T Default)
|
internal static class Helpers<T>
|
||||||
{
|
{
|
||||||
var item = CacheGetItem(Database, Scope, Key);
|
private static readonly IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
|
||||||
|
|
||||||
if (item == null)
|
internal static T GetValue(DiscoDataContext Database, string Scope, string Key, T Default)
|
||||||
return Default;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (item.Item2 != null && item.Item2.GetType() == typeof(T))
|
var item = CacheGetItem(Database, Scope, Key);
|
||||||
{
|
|
||||||
// Return Cached Item
|
if (item == null)
|
||||||
return (T)item.Item2;
|
return Default;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Convert Serialized Item
|
if (item.Item2 != null && item.Item2.GetType() == typeof(T))
|
||||||
Type itemType = typeof(T);
|
|
||||||
object itemValue;
|
|
||||||
|
|
||||||
if (itemType == typeof(string))
|
|
||||||
{
|
{
|
||||||
// string
|
// Return Cached Item
|
||||||
itemValue = item.Item1.Value;
|
return (T)item.Item2;
|
||||||
}
|
|
||||||
else if (itemType == typeof(object))
|
|
||||||
{
|
|
||||||
// object
|
|
||||||
itemValue = item.Item1.Value;
|
|
||||||
}
|
|
||||||
else if (IsConvertableFromString(itemType))
|
|
||||||
{
|
|
||||||
// IConvertable
|
|
||||||
itemValue = Convert.ChangeType(item.Item1.Value, itemType);
|
|
||||||
}
|
|
||||||
else if (itemType.BaseType != null && itemType.BaseType == typeof(Enum))
|
|
||||||
{
|
|
||||||
// Enum
|
|
||||||
itemValue = Enum.Parse(typeof(T), item.Item1.Value);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// JSON Deserialize
|
// Convert Serialized Item
|
||||||
itemValue = JsonConvert.DeserializeObject<T>(item.Item1.Value);
|
Type itemType = typeof(T);
|
||||||
|
object itemValue;
|
||||||
|
|
||||||
|
if (itemType == typeof(string))
|
||||||
|
{
|
||||||
|
// string
|
||||||
|
itemValue = item.Item1.Value;
|
||||||
|
}
|
||||||
|
else if (itemType == typeof(object))
|
||||||
|
{
|
||||||
|
// object
|
||||||
|
itemValue = item.Item1.Value;
|
||||||
|
}
|
||||||
|
else if (IsConvertableFromString(itemType))
|
||||||
|
{
|
||||||
|
// IConvertable
|
||||||
|
itemValue = Convert.ChangeType(item.Item1.Value, itemType);
|
||||||
|
}
|
||||||
|
else if (itemType.BaseType != null && itemType.BaseType == typeof(Enum))
|
||||||
|
{
|
||||||
|
// Enum
|
||||||
|
itemValue = Enum.Parse(typeof(T), item.Item1.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// JSON Deserialize
|
||||||
|
itemValue = JsonConvert.DeserializeObject<T>(item.Item1.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Item in Cache
|
||||||
|
SetItemTypeValue(item, itemValue);
|
||||||
|
|
||||||
|
return (T)itemValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Item in Cache
|
|
||||||
SetItemTypeValue(item, itemValue);
|
|
||||||
|
|
||||||
return (T)itemValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
internal static void SetValue(DiscoDataContext Database, string Scope, string Key, T Value)
|
||||||
internal static void SetValue<T>(DiscoDataContext Database, string Scope, string Key, T Value)
|
{
|
||||||
{
|
Type valueType = typeof(T);
|
||||||
Type valueType = typeof(T);
|
string stringValue;
|
||||||
string stringValue;
|
|
||||||
|
|
||||||
if (Value == null)
|
if (comparer.Equals(Value, default(T)))
|
||||||
{
|
{
|
||||||
stringValue = null;
|
stringValue = null;
|
||||||
}
|
}
|
||||||
else if (valueType == typeof(object))
|
else if (valueType == typeof(object))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(string.Format("Cannot serialize the configuration item [{0}].[{1}] which defines a type of [System.Object]", Scope, Key), "Value");
|
throw new ArgumentException(string.Format("Cannot serialize the configuration item [{0}].[{1}] which defines a type of [System.Object]", Scope, Key), "Value");
|
||||||
}
|
}
|
||||||
else if (IsConvertableFromString(valueType))
|
else if (IsConvertableFromString(valueType))
|
||||||
{
|
{
|
||||||
// string or supports IConvertable
|
// string or supports IConvertable
|
||||||
stringValue = Value.ToString();
|
stringValue = Value.ToString();
|
||||||
}
|
}
|
||||||
else if (valueType.BaseType != null && valueType.BaseType == typeof(Enum))
|
else if (valueType.BaseType != null && valueType.BaseType == typeof(Enum))
|
||||||
{
|
{
|
||||||
// Enum
|
// Enum
|
||||||
stringValue = Value.ToString();
|
stringValue = Value.ToString();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// JSON Serialize
|
// JSON Serialize
|
||||||
stringValue = JsonConvert.SerializeObject(Value);
|
stringValue = JsonConvert.SerializeObject(Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
CacheSetItem(Database, Scope, Key, stringValue, Value);
|
CacheSetItem(Database, Scope, Key, stringValue, Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Cache Helpers
|
#region Cache Helpers
|
||||||
|
|||||||
Reference in New Issue
Block a user