Browse Source

Addressed thread safety in Session providers

master
Daniel Gyulai 4 years ago
parent
commit
e0868ac6c6
  1. 1
      CoviDok/BLL/Session/DummyProvider.cs
  2. 8
      CoviDok/BLL/Session/RedisProvider.cs

1
CoviDok/BLL/Session/DummyProvider.cs

@ -4,6 +4,7 @@ using System.Text;
namespace CoviDok.BLL namespace CoviDok.BLL
{ {
// Class is not thread safe, should only be used for testing
class DummyProvider : ISessionProvider class DummyProvider : ISessionProvider
{ {
private readonly Dictionary<string, string> dict; private readonly Dictionary<string, string> dict;

8
CoviDok/BLL/Session/RedisProvider.cs

@ -7,23 +7,27 @@ namespace CoviDok.BLL
{ {
class RedisProvider : ISessionProvider class RedisProvider : ISessionProvider
{ {
private readonly IDatabase conn; // The Multiplexer is thread safe, connections are not
private readonly ConnectionMultiplexer muxer;
public RedisProvider(string host) public RedisProvider(string host)
{ {
conn = ConnectionMultiplexer.Connect(host).GetDatabase(); muxer = ConnectionMultiplexer.Connect(host);
} }
public void Del(string key) public void Del(string key)
{ {
IDatabase conn = muxer.GetDatabase();
conn.KeyDelete(key); conn.KeyDelete(key);
} }
public string Get(string key) public string Get(string key)
{ {
IDatabase conn = muxer.GetDatabase();
return conn.StringGet(key); return conn.StringGet(key);
} }
public void Set(string key, string value) public void Set(string key, string value)
{ {
IDatabase conn = muxer.GetDatabase();
conn.StringSet(key, value); conn.StringSet(key, value);
} }
} }

Loading…
Cancel
Save