Xiajijian
2025-10-30 9e976a6cd2f5f6011284df9cedd9e2dd96b9f68e
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
package com.ard.work.component.netty.cache;
 
import io.netty.channel.Channel;
 
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 边缘服务器tcp服务器缓存
 *
 * @author liusuyi
 * @date 2024年11月01日13:53
 **/
public class TyTcpServerCache {
    /**
     * 以 serverId 作为唯一 key 存储 Channel
     */
    private static Map<String, Channel> channels = new ConcurrentHashMap<>();
 
    //存储通道
    public static void putChannel(String serverId, Channel channel) {
        channels.put(serverId, channel);
    }
 
    //获取通道
    public static Channel getChannel(String serverId) {
        return channels.get(serverId);
    }
 
    //删除通道
    public static void removeChannel(String serverId) {
        channels.remove(serverId);
    }
 
    //获取所有
    public static Collection<Channel> getChannels() {
        return Collections.unmodifiableCollection(channels.values());
    }
 
 
    /**
     * 以 serverId 作为唯一 key 存储 连接状态
     */
    private static final ConcurrentHashMap<String, Boolean> tcpServerCache = new ConcurrentHashMap<>();
 
    // 存储TCP服务连接状态到缓存
    public static void putTcpServerCache(String serverId, Boolean state) {
        tcpServerCache.put(serverId, state);
    }
 
    // 从缓存中获取TCP服务连接状态
    public static Boolean getTcpServerCache(String serverId) {
        return tcpServerCache.getOrDefault(serverId, false);
    }
 
    // 删除缓存中的TCP服务连接状态
    public static void removeTcpServerCache(String serverId) {
        tcpServerCache.remove(serverId);
    }
 
    public static Map<String, Boolean> getTcpServerCacheMap() {
        return new HashMap<>(tcpServerCache); // 返回一个副本,避免外部修改原始缓存
    }
 
}