package redis import ( "context" "github.com/redis/go-redis/v9" "github.com/sirupsen/logrus" ) var ctx = context.Background() var rdb *redis.Client func Init() { rdb = redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) err := rdb.Ping(ctx).Err() if err != nil { logrus.Fatalln("Redis ping failed") } } func AddUserWebSocketSessionId(userId string, wsSessionId string) { cmd := rdb.LPush(ctx, userId, wsSessionId) logrus.Println("b", cmd) } func RemoveUserWebSocketSessionId(userId string, wsSessionId string) { cmd := rdb.LRem(ctx, userId, -1, wsSessionId) logrus.Println("rem", cmd) } func IsUserConnectedToAnyWebSocketServer(userId string) bool { cmd := rdb.Exists(ctx, userId) logrus.Println("exists b", cmd) return cmd.Val() == 1 } func ExistsUserWebSocketSessionId(userId string, wsSessionId string) bool { wsSessions := rdb.LRange(ctx, userId, 0, -1) logrus.Println("found ws", wsSessions.Val()) return isWsSessionIdInList(wsSessions.Val(), wsSessionId) } func isWsSessionIdInList(wsSessions []string, wsSessionId string) bool { for _, item := range wsSessions { if item == wsSessionId { return true } } return false }