From e0868ac6c6de4bbbd1911ef8a8bfce186360f204 Mon Sep 17 00:00:00 2001
From: Daniel Gyulai <gydani4@hotmail.hu>
Date: Thu, 12 Nov 2020 00:02:40 +0100
Subject: [PATCH] Addressed thread safety in Session providers

---
 CoviDok/BLL/Session/DummyProvider.cs | 1 +
 CoviDok/BLL/Session/RedisProvider.cs | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/CoviDok/BLL/Session/DummyProvider.cs b/CoviDok/BLL/Session/DummyProvider.cs
index f28a92b..02f5f1f 100644
--- a/CoviDok/BLL/Session/DummyProvider.cs
+++ b/CoviDok/BLL/Session/DummyProvider.cs
@@ -4,6 +4,7 @@ using System.Text;
 
 namespace CoviDok.BLL
 {
+    // Class is not thread safe, should only be used for testing
     class DummyProvider : ISessionProvider
     {
         private readonly Dictionary<string, string> dict;
diff --git a/CoviDok/BLL/Session/RedisProvider.cs b/CoviDok/BLL/Session/RedisProvider.cs
index 4e12fc9..9a82f93 100644
--- a/CoviDok/BLL/Session/RedisProvider.cs
+++ b/CoviDok/BLL/Session/RedisProvider.cs
@@ -7,23 +7,27 @@ namespace CoviDok.BLL
 {
     class RedisProvider : ISessionProvider
     {
-        private readonly IDatabase conn;
+        // The Multiplexer is thread safe, connections are not
+        private readonly ConnectionMultiplexer muxer;
         public RedisProvider(string host)
         {
-            conn = ConnectionMultiplexer.Connect(host).GetDatabase();
+            muxer = ConnectionMultiplexer.Connect(host);
         }
         public void Del(string key)
         {
+            IDatabase conn = muxer.GetDatabase();
             conn.KeyDelete(key);
         }
 
         public string Get(string key)
         {
+            IDatabase conn = muxer.GetDatabase();
             return conn.StringGet(key);
         }
 
         public void Set(string key, string value)
         {
+            IDatabase conn = muxer.GetDatabase();
             conn.StringSet(key, value);
         }
     }