using System;
using System.Collections.Generic;
namespace Noikoio.RegexBot.Feature.AutoRespond
{
///
/// Stores rate limit settings and caches.
///
class RateLimitCache
{
public const ushort DefaultTimeout = 30; // TODO make configurable
private readonly ushort _timeout;
private Dictionary _cache;
public ushort Timeout => _timeout;
///
/// Sets up a new instance of .
///
public RateLimitCache(ushort timeout)
{
_timeout = timeout;
_cache = new Dictionary();
}
///
/// Adds a cache item corersponding to the given ID.
/// Items added to cache will be removed after the number of seconds specified in .
///
/// The ID to add to the cache.
/// True on success. False if the given ID already exists.
public bool AddUsage(ulong id)
{
Clean();
if (_cache.ContainsKey(id)) return false;
_cache.Add(id, DateTime.Now);
return true;
}
private void Clean()
{
var now = DateTime.Now;
var clean = new Dictionary();
foreach (var kp in _cache)
{
if (kp.Value.AddSeconds(Timeout) > now)
{
// Copy items that have not yet timed out to the new dictionary
clean.Add(kp.Key, kp.Value);
}
}
_cache = clean;
}
}
}