Browse Source

Added locks to ConfigurationCache to prevent concurrent access exceptions;

Kenric Nugteren 5 days ago
parent
commit
6fb688592a
1 changed files with 32 additions and 20 deletions
  1. 32 20
      InABox.Core/Configuration/ConfigurationCache.cs

+ 32 - 20
InABox.Core/Configuration/ConfigurationCache.cs

@@ -40,21 +40,24 @@ namespace InABox.Configuration
         {
             Log(LogType.Information, $"- Checking {typeof(T)}({type},{section})");
             var _cache = GetCache(type);
-            if (_cache.ContainsKey(typeof(T)))
+            lock (_cache)
             {
-                var subcache = _cache[typeof(T)];
-                if (subcache.ContainsKey(section))
+                if (_cache.ContainsKey(typeof(T)))
                 {
-                    var data = subcache[section];
-                    if (!string.IsNullOrWhiteSpace(data))
+                    var subcache = _cache[typeof(T)];
+                    if (subcache.ContainsKey(section))
                     {
-                        Log(LogType.Information, $"- Checking: Found {typeof(T)}({type},{section}) -> {subcache[section]}");
-                        var result = Serialization.Deserialize<T>(subcache[section]);
-                        if(result is BaseObject obj)
+                        var data = subcache[section];
+                        if (!string.IsNullOrWhiteSpace(data))
                         {
-                            obj.CommitChanges();
+                            Log(LogType.Information, $"- Checking: Found {typeof(T)}({type},{section}) -> {subcache[section]}");
+                            var result = Serialization.Deserialize<T>(subcache[section]);
+                            if(result is BaseObject obj)
+                            {
+                                obj.CommitChanges();
+                            }
+                            return result;
                         }
-                        return result;
                     }
                 }
             }
@@ -66,23 +69,29 @@ namespace InABox.Configuration
         {
             Log(LogType.Information, $"- Adding {typeof(T)}({type},{section})");
             var _cache = GetCache(type);
-            if (!_cache.ContainsKey(typeof(T)))
-                _cache[typeof(T)] = new Dictionary<string, string>();
-            var subcache = _cache[typeof(T)];
-            subcache[section] = Serialization.Serialize(config);
+            lock (_cache)
+            {
+                if (!_cache.ContainsKey(typeof(T)))
+                    _cache[typeof(T)] = new Dictionary<string, string>();
+                var subcache = _cache[typeof(T)];
+                subcache[section] = Serialization.Serialize(config);
+            }
         }
 
         public static void Clear<T>(ConfigurationCacheType type, string section)
         {
             Log(LogType.Information, $"- Clearing {typeof(T)}({type},{section})");
             var _cache = GetCache(type);
-            if (_cache.ContainsKey(typeof(T)))
+            lock (_cache)
             {
-                var subcache = _cache[typeof(T)];
-                if (subcache.ContainsKey(section))
+                if (_cache.ContainsKey(typeof(T)))
                 {
-                    Log(LogType.Information, $"- Clearing: Removing {typeof(T)}({type},{section})");
-                    subcache[section] = "";
+                    var subcache = _cache[typeof(T)];
+                    if (subcache.ContainsKey(section))
+                    {
+                        Log(LogType.Information, $"- Clearing: Removing {typeof(T)}({type},{section})");
+                        subcache[section] = "";
+                    }
                 }
             }
         }
@@ -95,7 +104,10 @@ namespace InABox.Configuration
         public static void ClearAll(ConfigurationCacheType type)
         {
             var _cache = GetCache(type);
-            _cache.Clear();
+            lock(_cache)
+            {
+                _cache.Clear();
+            }
         }
     }
 }