Is this ConcurrentDictionary ThreadSafe?
private static ConcurrentDictionary<string, DateTime> dictionary= new ConcurrentDictionary<string, DateTime>();
public bool TextInputRecently(string text)
{
dictionary = new ConcurrentDictionary<string, DateTime>( dictionary.Where(pair => pair.Value.Ticks >= DateTime.Now.AddMinutes(-5).Ticks)
.ToDictionary(pair => pair.Key,
pair => pair.Value));
if (dictionary.ContainsKey(text))
return true;
dictionary.TryAdd(text, DateTime.Now);
return false;
}
I am thinking that it is not as the dictionary could be recreated whilst another thread is checking if the key exists.
Would i be better off looping through the dictionary removing the out of date values?