2019-07-03 18:31:18 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class InMemoryStorageService : IStorageService
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<string, string> _dict = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
public Task<T> GetAsync<T>(string key)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_dict.ContainsKey(key))
|
2019-07-03 18:31:18 +02:00
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(default(T));
|
|
|
|
|
}
|
|
|
|
|
return Task.FromResult(JsonConvert.DeserializeObject<T>(_dict[key]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SaveAsync<T>(string key, T obj)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (obj == null)
|
2019-07-03 18:31:18 +02:00
|
|
|
|
{
|
|
|
|
|
return RemoveAsync(key);
|
|
|
|
|
}
|
|
|
|
|
_dict.Add(key, JsonConvert.SerializeObject(obj));
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task RemoveAsync(string key)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_dict.ContainsKey(key))
|
2019-07-03 18:31:18 +02:00
|
|
|
|
{
|
|
|
|
|
_dict.Remove(key);
|
|
|
|
|
}
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|