forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryAuthRepository.cs
More file actions
210 lines (173 loc) · 6.78 KB
/
InMemoryAuthRepository.cs
File metadata and controls
210 lines (173 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ServiceStack.Common;
using ServiceStack.Utils;
namespace ServiceStack.ServiceInterface.Auth
{
/// <summary>
/// Thread-safe In memory UserAuth data store so it can be used without a dependency on Redis.
/// </summary>
public class InMemoryAuthRepository : RedisAuthRepository
{
public static readonly InMemoryAuthRepository Instance = new InMemoryAuthRepository();
protected Dictionary<string, HashSet<string>> Sets { get; set; }
protected Dictionary<string, Dictionary<string, string>> Hashes { get; set; }
internal List<IClearable> TrackedTypes = new List<IClearable>();
class TypedData<T> : IClearable
{
internal static TypedData<T> Instance = new TypedData<T>();
private TypedData()
{
lock (InMemoryAuthRepository.Instance.TrackedTypes)
InMemoryAuthRepository.Instance.TrackedTypes.Add(this);
}
internal readonly List<T> Items = new List<T>();
internal int Sequence = 0;
public void Clear()
{
lock (Items) Items.Clear();
Interlocked.CompareExchange(ref Sequence, 0, Sequence);
}
}
public InMemoryAuthRepository() : base(new InMemoryManagerFacade(Instance))
{
this.Sets = new Dictionary<string, HashSet<string>>();
this.Hashes = new Dictionary<string, Dictionary<string, string>>();
}
class InMemoryManagerFacade : IRedisClientManagerFacade
{
private readonly InMemoryAuthRepository root;
public InMemoryManagerFacade(InMemoryAuthRepository root)
{
this.root = root;
}
public IRedisClientFacade GetClient()
{
return new InMemoryClientFacade(root);
}
public void Clear()
{
lock (Instance.Sets) Instance.Sets.Clear();
lock (Instance.Hashes) Instance.Hashes.Clear();
lock (Instance.TrackedTypes) Instance.TrackedTypes.ForEach(x => x.Clear());
}
}
class InMemoryClientFacade : IRedisClientFacade
{
private readonly InMemoryAuthRepository root;
public InMemoryClientFacade(InMemoryAuthRepository root)
{
this.root = root;
}
class InMemoryTypedClientFacade<T> : ITypedRedisClientFacade<T>
{
private readonly InMemoryAuthRepository root;
public InMemoryTypedClientFacade(InMemoryAuthRepository root)
{
this.root = root;
}
public int GetNextSequence()
{
return Interlocked.Increment(ref TypedData<T>.Instance.Sequence);
}
public T GetById(object id)
{
if (id == null) return default(T);
lock (TypedData<T>.Instance.Items)
{
return TypedData<T>.Instance.Items.FirstOrDefault(x => id.ToString() == x.ToId().ToString());
}
}
public List<T> GetByIds(IEnumerable ids)
{
var idsSet = new HashSet<object>();
foreach (var id in ids) idsSet.Add(id.ToString());
lock (TypedData<T>.Instance.Items)
{
return TypedData<T>.Instance.Items.Where(x => idsSet.Contains(x.ToId().ToString())).ToList();
}
}
}
public HashSet<string> GetAllItemsFromSet(string setId)
{
lock (root.Sets)
{
HashSet<string> set;
return root.Sets.TryGetValue(setId, out set) ? set : new HashSet<string>();
}
}
public void Store<T>(T item)
{
if (Equals(item, default(T))) return;
lock (TypedData<T>.Instance.Items)
{
for (var i = 0; i < TypedData<T>.Instance.Items.Count; i++)
{
var o = TypedData<T>.Instance.Items[i];
if (o.ToId().ToString() != item.ToId().ToString()) continue;
TypedData<T>.Instance.Items[i] = item;
return;
}
TypedData<T>.Instance.Items.Add(item);
}
}
public string GetValueFromHash(string hashId, string key)
{
hashId.ThrowIfNull("hashId");
key.ThrowIfNull("key");
lock (root.Hashes)
{
Dictionary<string, string> hash;
if (!root.Hashes.TryGetValue(hashId, out hash)) return null;
string value;
hash.TryGetValue(key, out value);
return value;
}
}
public void SetEntryInHash(string hashId, string key, string value)
{
hashId.ThrowIfNull("hashId");
key.ThrowIfNull("key");
lock (root.Hashes)
{
Dictionary<string, string> hash;
if (!root.Hashes.TryGetValue(hashId, out hash))
root.Hashes[hashId] = hash = new Dictionary<string, string>();
hash[key] = value;
}
}
public void RemoveEntryFromHash(string hashId, string key)
{
hashId.ThrowIfNull("hashId");
key.ThrowIfNull("key");
lock (root.Hashes)
{
Dictionary<string, string> hash;
if (!root.Hashes.TryGetValue(hashId, out hash))
root.Hashes[hashId] = hash = new Dictionary<string, string>();
hash.Remove(key);
}
}
public void AddItemToSet(string setId, string item)
{
lock (root.Sets)
{
HashSet<string> set;
if (!root.Sets.TryGetValue(setId, out set))
root.Sets[setId] = set = new HashSet<string>();
set.Add(item);
}
}
public ITypedRedisClientFacade<T> As<T>()
{
return new InMemoryTypedClientFacade<T>(root);
}
public void Dispose()
{
}
}
}
}