Repository navigation
Expand file tree
/
Copy pathAgentStorageInMemory.cs
More file actions
72 lines (61 loc) · 1.74 KB
/
Copy pathAgentStorageInMemory.cs
File metadata and controls
72 lines (61 loc) · 1.74 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
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core
{
/// <summary>
/// Save agent instance into memory.
/// Caution: Data will be lost once application restarts.
/// </summary>
public class AgentStorageInMemory<TAgent> : IAgentStorage<TAgent> where TAgent : AgentBase
{
private static Dictionary<string, TAgent> agents;
public AgentStorageInMemory()
{
if (agents == null) agents = new Dictionary<string, TAgent>();
}
public async Task<TAgent> FetchById(string agentId)
{
if (agents.ContainsKey(agentId))
{
return agents[agentId];
}
else
{
return null;
}
}
public async Task<TAgent> FetchByName(string agentName)
{
var data = agents.FirstOrDefault(x => x.Value.Name == agentName);
return data.Value;
}
public async Task<bool> Persist(TAgent agent)
{
if (String.IsNullOrEmpty(agent.Id))
{
agent.Id = Guid.NewGuid().ToString();
agents[agent.Id] = agent;
}
else
{
agents[agent.Id] = agent;
}
return true;
}
public async Task<int> PurgeAllAgents()
{
int count = agents.Count;
agents.Clear();
return count;
}
public async Task<List<TAgent>> Query()
{
return agents.Select(x => x.Value).ToList();
}
}
}