创建单例缓存map集合
无情
阅读:664
2021-04-01 11:15:57
评论:0
1.项目中需要把数据和websocket链接缓存到map中
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import okhttp3.WebSocket;
public class BufferMap {
private static BufferMap bufferMap;
private static Map<String, String> mapChacheMsgItems;
private static Map<String, WebSocket> webSocketMap;
private BufferMap() {
mapChacheMsgItems = new ConcurrentHashMap<String, String>();
webSocketMap = new ConcurrentHashMap<String, WebSocket>();
}
public static BufferMap getInstance() {
if (bufferMap == null) {
synchronized (BufferMap.class) {
if (bufferMap == null) {
bufferMap = new BufferMap();
}
}
}
return bufferMap;
}
public WebSocket getWebSocket(String key) {
if (webSocketMap.containsKey(key)) {
return webSocketMap.get(key);
}
return null;
}
public WebSocket putWebSocket(String key, WebSocket webSocket) {
return webSocketMap.put(key, webSocket);
}
public Map<String, String> getChacheMsgItems() {
return mapChacheMsgItems;
}
public String getCacheMsgItem(String key) {
if (mapChacheMsgItems.containsKey(key)) {
return mapChacheMsgItems.get(key);
}
return null;
}
public String putMapChacheMsgItems(String key, String msg) {
return mapChacheMsgItems.put(key, msg);
}
public void removeCacheMsgItem(String key) {
if (mapChacheMsgItems.containsKey(key)) {
mapChacheMsgItems.remove(key);
}
}
public void clearCache() {
mapChacheMsgItems.clear();
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。