BotSharp/BotSharp.Core/AgentStorageInMemory.cs
geffzhang 83b9a2347e refactor di and modify async/await
feat : refactor di and modify async/await
2018-10-05 14:39:39 +08:00

73 lines
1.7 KiB
C#

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();
}
}
}