Cache default values

This commit is contained in:
Mbucari 2023-01-09 16:05:55 -07:00
parent 2b243a6934
commit 50a8c7508a

View File

@ -38,9 +38,10 @@ namespace FileManager
if (!stringCache.ContainsKey(propertyName)) if (!stringCache.ContainsKey(propertyName))
{ {
var jObject = readFile(); var jObject = readFile();
if (!jObject.ContainsKey(propertyName)) if (jObject.ContainsKey(propertyName))
return defaultValue;
stringCache[propertyName] = jObject[propertyName].Value<string>(); stringCache[propertyName] = jObject[propertyName].Value<string>();
else
stringCache[propertyName] = defaultValue;
} }
return stringCache[propertyName]; return stringCache[propertyName];
@ -50,12 +51,16 @@ namespace FileManager
{ {
var obj = GetObject(propertyName); var obj = GetObject(propertyName);
if (obj is null) return defaultValue; if (obj is null)
{
objectCache[propertyName] = defaultValue;
return defaultValue;
}
if (obj.GetType().IsAssignableTo(typeof(T))) return (T)obj; if (obj.GetType().IsAssignableTo(typeof(T))) return (T)obj;
if (obj is JObject jObject) return jObject.ToObject<T>(); if (obj is JObject jObject) return jObject.ToObject<T>();
if (obj is JValue jValue) if (obj is JValue jValue)
{ {
if (jValue.Type == JTokenType.String && typeof(T).IsAssignableTo(typeof(Enum))) if (typeof(T).IsAssignableTo(typeof(Enum)))
{ {
return return
Enum.TryParse(typeof(T), jValue.Value<string>(), out var enumVal) Enum.TryParse(typeof(T), jValue.Value<string>(), out var enumVal)