zoukankan      html  css  js  c++  java
  • [.Net] Dictionary<T, HashSet<string>>

    using System.Collections.Generic;
    using System.Linq;
    
    namespace BasicChat
    {
        public class ConnectionMapping<T>
        {
            private readonly Dictionary<T, HashSet<string>> _connections =
                new Dictionary<T, HashSet<string>>();
    
            public int Count
            {
                get
                {
                    return _connections.Count;
                }
            }
    
            public void Add(T key, string connectionId)
            {
                lock (_connections)
                {
                    HashSet<string> connections;
                    if (!_connections.TryGetValue(key, out connections))
                    {
                        connections = new HashSet<string>();
                        _connections.Add(key, connections);
                    }
    
                    lock (connections)
                    {
                        connections.Add(connectionId);
                    }
                }
            }
    
            public IEnumerable<string> GetConnections(T key)
            {
                HashSet<string> connections;
                if (_connections.TryGetValue(key, out connections))
                {
                    return connections;
                }
    
                return Enumerable.Empty<string>();
            }
    
            public void Remove(T key, string connectionId)
            {
                lock (_connections)
                {
                    HashSet<string> connections;
                    if (!_connections.TryGetValue(key, out connections))
                    {
                        return;
                    }
    
                    lock (connections)
                    {
                        connections.Remove(connectionId);
    
                        if (connections.Count == 0)
                        {
                            _connections.Remove(key);
                        }
                    }
                }
            }
        }
    }

    Ref:

    Mapping SignalR Users to Connections

  • 相关阅读:
    html 注释和特殊字符
    html 锚点链接
    html 链接标签
    spring 利用工厂模式解耦
    html 路径
    html 图像标签
    html div和span标签
    html 文本格式化标签
    P5358 [SDOI2019]快速查询
    luoguP2679 子串
  • 原文地址:https://www.cnblogs.com/zhonglinchen/p/14414329.html
Copyright © 2011-2022 走看看