private void UpdateSessionCache(IIMapServerSettings setting) { if (!ImapSessionCache.ContainsKey(setting)) // Add if session doesn't already exist { ImapSessionCache.TryAdd(setting, _imapMailReader.CreateConnection(setting)); } else { RefreshSessionIfExpired(setting); } }
private void RefreshSessionIfExpired(IIMapServerSettings setting) { var session = ImapSessionCache.FirstOrDefault(s => s.Key == setting).Value; if (session != null && !session.IsConnected()) { IImapMailReader removed; if (ImapSessionCache.TryRemove(setting, out removed) && removed != null) { ImapSessionCache.TryAdd(setting, _imapMailReader.CreateConnection(setting)); } } }