shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.walker.tcp.support;
 
import com.walker.infrastructure.utils.StringUtils;
import com.walker.tcp.Connection;
import com.walker.tcp.ConnectionCache;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 基于内存实现的连接缓存定义。
 * @date 2023-09-19
 */
public class MemoryConnectionCache implements ConnectionCache {
 
    // 连接缓存,key = sessionId, value = Connection
    private final Map<String, Connection> cached = new ConcurrentHashMap<>(128);
 
    // 通道ID与绑定用户名称的对应关系,key = 绑定业务名称,value = 通道ID
    private Map<String, String> idNameCached = new ConcurrentHashMap<>(128);
 
    @Override
    public void putConnection(Connection connection) {
        String id = connection.getId();
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException("connection id 必须存在");
        }
        cached.put(id, connection);
        idNameCached.put(connection.getName(), id);
    }
 
    @Override
    public void removeConnection(String id) {
        Connection connection = this.cached.get(id);
        if(connection != null){
            this.idNameCached.remove(connection.getName());
//            // 2023-10-17,断开物理连接
            // 2023-10-18 移除代码,看是否聊天正常(财政厅运维)
//            connection.disconnect();
        }
        this.cached.remove(id);
    }
 
    @Override
    public void removeConnection(String id, Connection connection) {
        if(connection == null){
            connection = this.cached.get(id);
        }
        if(connection != null){
            this.idNameCached.remove(connection.getName());
//            // 2023-10-17,断开物理连接
            // 2023-10-18 移除代码,看是否聊天正常(财政厅运维)
//            connection.disconnect();
        }
        this.cached.remove(id);
    }
 
    @Override
    public void updateConnection(Connection connection) {
        this.cached.put(connection.getId(), connection);
    }
 
    @Override
    public Connection getConnection(String id) {
        return this.cached.get(id);
    }
 
    @Override
    public Connection getConnectionByName(String name) {
        String id = this.idNameCached.get(name);
        if(StringUtils.isEmpty(id)){
            return null;
        }
        return this.cached.get(id);
    }
 
    @Override
    public String getIdByName(String name) {
        return this.idNameCached.get(name);
    }
 
    @Override
    public List<Connection> getAllConnectionList() {
        List<Connection> result = new ArrayList<>();
        for(Connection conn : cached.values()){
            result.add(conn);
        }
        return result;
    }
 
    @Override
    public List<Connection> getAllConnectionListBy(int engineId) {
        List<Connection> result = new ArrayList<>();
        for(Connection conn : cached.values()){
            if(conn.getEngineId() == engineId){
                result.add(conn);
            }
        }
        return result;
    }
}