chaosbot/ChaosBot/WebServer/AccessTokenCache.cs

49 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using ChaosBot.Services;
namespace ChaosBot.WebServer
{
public class AccessTokenCache : ICache<string, string>
{
private Dictionary<string, Tuple<string, DateTime>> _cache = new Dictionary<string, Tuple<string, DateTime>>();
public void Invalidate()
{
DateTime now = DateTime.Now;
_cache = _cache
.Where(v => v.Value.Item2 > now)
.ToDictionary(x => x.Key, x => x.Value);
}
public void Add(string key, string value, DateTime expires)
{
_cache.Add(key, new Tuple<string, DateTime>(value, expires));
}
public void Remove(string key)
{
_cache.Remove(key);
}
public string Get(string key)
{
if (!_cache.TryGetValue(key, out Tuple<string, DateTime> data))
throw new KeyNotFoundException();
return data.Item1;
}
public bool HasKey(string key)
{
return _cache.ContainsKey(key);
}
public List<string> GetKeys()
{
return _cache.Keys.ToList();
}
}
}