public bool AddReplace(string key, object val)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key));
lock (CacheLock)
{
if (Cache.Count >= Capacity)
{
Cache = Cache.OrderBy(x => x.Item3).Skip(EvictCount).ToList();
}
List<Tuple<string, object, DateTime>> dupes = new List<Tuple<string, object, DateTime>>();
if (Cache.Count > 0) dupes = Cache.Where(x => x.Item1.ToLower() == key).ToList();
else dupes = null;
if (dupes == null)
{
#region New-Entry
Cache.Add(new Tuple<string, object, DateTime>(key, val, DateTime.Now));
return true;
#endregion
}
else if (dupes.Count > 0)
{
#region Duplicate-Entries-Exist
foreach (Tuple<string, object, DateTime> curr in dupes)
{
Cache.Remove(curr);
}
Cache.Add(new Tuple<string, object, DateTime>(key, val, DateTime.Now));
return true;
#endregion
}
else
{
#region New-Entry
Cache.Add(new Tuple<string, object, DateTime>(key, val, DateTime.Now));
return true;
#endregion
}
}
}