mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
in memory storage service
This commit is contained in:
parent
ea745665c8
commit
8b7ac179fa
1 changed files with 40 additions and 0 deletions
40
src/Core/Services/InMemoryStorageService.cs
Normal file
40
src/Core/Services/InMemoryStorageService.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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)
|
||||
{
|
||||
if(!_dict.ContainsKey(key))
|
||||
{
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(_dict[key]));
|
||||
}
|
||||
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
if(obj == null)
|
||||
{
|
||||
return RemoveAsync(key);
|
||||
}
|
||||
_dict.Add(key, JsonConvert.SerializeObject(obj));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
if(_dict.ContainsKey(key))
|
||||
{
|
||||
_dict.Remove(key);
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue