From 66e189d24786cbc24a7285d57a09f316e6ef124f Mon Sep 17 00:00:00 2001 From: GitProtogen Date: Mon, 16 Mar 2026 13:49:11 +0100 Subject: [PATCH] add cache --- cache.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ strcut.go | 21 +++++++++++++++++++++ wsServer.go | 6 ++++-- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cache.go create mode 100644 strcut.go diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..95260ab --- /dev/null +++ b/cache.go @@ -0,0 +1,44 @@ +package main + +import "sync" + +var ( + mu sync.RWMutex + ChatGroups = make(map[uint32]ChatGroup) + Clients = make(map[uint32]Client) + ClientsMap = make(map[uint32]map[uint32]*Client) +) + +func AddGroupToCache(chatGroup *ChatGroup) { + mu.Lock() + defer mu.Unlock() + ChatGroups[chatGroup.Id] = *chatGroup + ClientsMap[chatGroup.Id] = make(map[uint32]*Client) +} + +func RemoveGroupFromCache(chatGroup *ChatGroup) { + mu.Lock() + defer mu.Unlock() + delete(ChatGroups, chatGroup.Id) + delete(ClientsMap, chatGroup.Id) +} + +func AddUserToCache(client *Client) { + mu.Lock() + defer mu.Unlock() + for _, groupIn := range client.Groups { + if clients, ok := ClientsMap[groupIn.Id]; ok { + clients[client.Id] = client + } + } +} + +func RemoveClientFromCache(client *Client) { + mu.Lock() + defer mu.Unlock() + for _, groupIn := range client.Groups { + if clients, ok := ClientsMap[groupIn.Id]; ok { + delete(clients, client.Id) + } + } +} diff --git a/strcut.go b/strcut.go new file mode 100644 index 0000000..c748b30 --- /dev/null +++ b/strcut.go @@ -0,0 +1,21 @@ +package main + +import "github.com/coder/websocket" + +type Client struct { + Password string + Name string + Pronouns string + Groups [12]*ChatGroup + Connection *websocket.Conn + Id uint32 + Color [3]byte + IsAuthenticated bool +} + +type ChatGroup struct { + Name string + Members [32]*Client + Id uint32 + Color [3]byte +} diff --git a/wsServer.go b/wsServer.go index 95b27e9..da666e6 100644 --- a/wsServer.go +++ b/wsServer.go @@ -38,5 +38,7 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request) } } -func handleUnauthenticatedMessage() {} -func handleAuthenticatedMessage() {} +func handleUnauthenticatedMessage() { + +} +func handleAuthenticatedMessage() {}