using System; using System.Collections.Generic; using System.Linq; namespace ArchestrAServices.ASBContract; public class SysAuthenticatorClientCache : SynchronizedKeyedCollection { private static readonly SysAuthenticatorClientCache clientConnections = new SysAuthenticatorClientCache(); private static object cacheLockObject = new object(); private SysAuthenticatorClientCache() { } public static IEnumerable GetAllClientAuthenticators() { List list = new List(); lock (cacheLockObject) { list.AddRange(clientConnections.Items); return list; } } public static void AddClientAuthenticator(SysAuthClientAuthentication clientAuthenticator) { lock (cacheLockObject) { clientConnections.Purge(); } if (clientAuthenticator == null) { return; } lock (cacheLockObject) { if (!clientConnections.Contains(clientAuthenticator.connectionID)) { clientConnections.Add(clientAuthenticator); } } } public static SysAuthClientAuthentication GetClientAuthenticator(Guid connectionId) { SysAuthClientAuthentication result = null; lock (cacheLockObject) { if (clientConnections.Contains(connectionId)) { result = clientConnections[connectionId]; } } return result; } public static SysAuthClientAuthentication RemoveClientAuthenticator(Guid connectionId) { SysAuthClientAuthentication result = GetClientAuthenticator(connectionId); lock (cacheLockObject) { if (clientConnections.Contains(connectionId)) { result = clientConnections[connectionId]; clientConnections.Remove(connectionId); } } return result; } protected void Purge() { foreach (Guid item in this.Where((SysAuthClientAuthentication item) => !item.IsOwnerAlive).Select(GetKeyForItem).ToList()) { Remove(item); } } protected override Guid GetKeyForItem(SysAuthClientAuthentication item) { return item.connectionID; } }