Skip to content

Commit 6a4e18c

Browse files
committed
storage interface and implementation
1 parent 74fe37d commit 6a4e18c

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ActiveLogin.Authentication.BankId.Core;
2+
3+
public interface IStateStorage
4+
{
5+
ValueTask<Guid> WriteAsync(object value);
6+
ValueTask<bool> TryReadAsync<T>(Guid key, out T? value);
7+
ValueTask<T?> RemoveAsync<T>(Guid key);
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace ActiveLogin.Authentication.BankId.Core;
4+
5+
public class InMemoryStateStorage : IStateStorage
6+
{
7+
private static readonly ConcurrentDictionary<Guid, object> _storage = new();
8+
9+
public ValueTask<Guid> WriteAsync(object value)
10+
{
11+
var key = Guid.NewGuid();
12+
_ = _storage.TryAdd(key, value);
13+
return new ValueTask<Guid>(key);
14+
}
15+
16+
public ValueTask<T?> RemoveAsync<T>(Guid key)
17+
{
18+
return _storage.TryRemove(key, out var value)
19+
? new ValueTask<T?>((T)value)
20+
: new ValueTask<T?>(default(T));
21+
}
22+
23+
public ValueTask<bool> TryReadAsync<T>(Guid key, out T? value)
24+
{
25+
if (_storage.TryGetValue(key, out var obj))
26+
{
27+
value = (T)obj;
28+
return new ValueTask<bool>(true);
29+
}
30+
31+
value = default;
32+
return new ValueTask<bool>(false);
33+
}
34+
}

0 commit comments

Comments
 (0)