46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"game/common/utils"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type UserGate struct {
|
||
|
GateName string `json:"gate_name"`
|
||
|
Token string `json:"token"`
|
||
|
OnlineTime utils.Time `json:"online"`
|
||
|
rdb *redis.Client
|
||
|
}
|
||
|
|
||
|
func NewUserGate() *UserGate {
|
||
|
return &UserGate{rdb: UserRedis}
|
||
|
}
|
||
|
|
||
|
func (u *UserGate) key(userId int64, gateId string) string {
|
||
|
return fmt.Sprintf("gateway:%v:%v", gateId, userId)
|
||
|
}
|
||
|
|
||
|
func (u *UserGate) Set(userId int64, gateId string, token string) {
|
||
|
u.GateName = gateId
|
||
|
u.Token = token
|
||
|
u.OnlineTime = utils.Time(time.Now())
|
||
|
u.rdb.Set(context.Background(), u.key(userId, gateId), utils.Marshal(u), time.Hour*24)
|
||
|
}
|
||
|
|
||
|
func (u *UserGate) Get(userId int64, gateId string) (*UserGate, error) {
|
||
|
s, err := u.rdb.Get(context.Background(), u.key(userId, gateId)).Result()
|
||
|
if err != nil {
|
||
|
return u, err
|
||
|
}
|
||
|
err = json.Unmarshal([]byte(s), u)
|
||
|
return u, err
|
||
|
}
|
||
|
|
||
|
func (u *UserGate) Del(userId int64, gateId string) {
|
||
|
_, _ = u.rdb.Del(context.Background(), u.key(userId, gateId)).Result()
|
||
|
}
|