-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryObjectStore.cs
More file actions
46 lines (38 loc) · 1.39 KB
/
InMemoryObjectStore.cs
File metadata and controls
46 lines (38 loc) · 1.39 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
using System.Collections.Concurrent;
namespace p42ObjectStores;
public class InMemoryObjectStore : BaseStore
{
readonly ConcurrentDictionary<string, object?> _reports = new();
public override int NumberOfObject(string? prefix = null)
{
return _reports.Count;
}
public override async Task<T?> Get<T>(string name, string? prefix = null) where T : class
{
string id = GetPath(name, "", prefix);
if (String.IsNullOrEmpty(id)) return null;
_reports.TryGetValue(id, out object? model);
if (model != null && typeof(T) == model.GetType())
return (T)model;
return null;
}
public override async Task<T?> Add<T>(T model, string name, string? prefix = null) where T : class
{
if (String.IsNullOrWhiteSpace(name)) return null;
string id = GetPath(name, "", prefix);
if (_reports.TryAdd(id, model)) return model;
return null;
}
public override bool Delete(string name, string? prefix = null)
{
return _reports.TryRemove(GetPath(name, "", prefix), out _);
}
public override bool Update<T>(string name, T model, string? prefix = null)
{
string id = GetPath(name, "", prefix);
if (String.IsNullOrEmpty(id) || model == null) return false;
if (!_reports.ContainsKey(id)) return false;
_reports[id] = model;
return true;
}
}