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); // 返回一个副本,避免外部修改原始缓存
|
}
|
|
}
|