From 10eb14e77f496c45beadf97d5818261c75282b77 Mon Sep 17 00:00:00 2001 From: tangmingyou Date: Tue, 21 Mar 2023 18:00:34 +0800 Subject: [PATCH] store with go-cache --- data/config.toml | 1 - go.mod | 1 + go.sum | 2 ++ internal/model/entity/user.go | 13 +++++---- internal/service/auth.go | 1 + internal/service/store/store.go | 52 +++++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 7 deletions(-) diff --git a/data/config.toml b/data/config.toml index 789f2e0..a1ec0a5 100644 --- a/data/config.toml +++ b/data/config.toml @@ -29,4 +29,3 @@ avatarPath = "data/avatar/" [game.offline] # 玩家掉线配置 # 在房间中 outRoom = 15 - diff --git a/go.mod b/go.mod index 763332f..de0e26e 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/o1egl/govatar v0.4.1 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/ugorji/go/codec v1.2.7 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect diff --git a/go.sum b/go.sum index c2c56c5..297dac9 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/o1egl/govatar v0.4.1 h1:RRzAxm52WpZMSEoWgAXrTcXWKhIUPpgpI54KP+UI0Ew= github.com/o1egl/govatar v0.4.1/go.mod h1:cSBJjpgYiKmQ8E+C4zNBcsbuDwy9UH4HS8BwE4m6JmQ= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= diff --git a/internal/model/entity/user.go b/internal/model/entity/user.go index 37d477a..cacfa4f 100644 --- a/internal/model/entity/user.go +++ b/internal/model/entity/user.go @@ -2,12 +2,13 @@ package entity type User struct { Model - Id int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - Username string `gorm:"column:username" json:"username"` // comment:用户名 - Password string `gorm:"column:password" json:"password"` // comment:密码 - Balance int32 `gorm:"column:balance" json:"balance"` // comment:余额 - Avatar string `gorm:"column:avatar" json:"avatar"` // comment:头像 - Version int64 `gorm:"column:version" json:"version"` // comment:更新版本锁 + Id int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + Username string `gorm:"column:username" json:"username"` // comment:用户名 + Password string `gorm:"column:password" json:"password"` // comment:密码 + Balance int32 `gorm:"column:balance;default:0" json:"balance"` // comment:余额 + Avatar string `gorm:"column:avatar" json:"avatar"` // comment:头像 + Version int32 `gorm:"column:version;default:0" json:"version"` // comment:更新版本锁 + TokenVersion int32 `gorm:"column:token_version;default:0" json:"tokenVersion"` // comment:token版本,重新登录后之前的token失效,登录丢到缓存中和token过期时间相同 } func (u *User) TableName() string { diff --git a/internal/service/auth.go b/internal/service/auth.go index e5073f5..09de09b 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -77,6 +77,7 @@ func Authorize(ctx *gin.Context) { Time: time.Now().UnixMilli(), Avatar: user.Avatar, } + token, err := EncodeSubject(sub) if err != nil { ctx.JSON(http.StatusUnauthorized, gin.H{"msg": err.Error()}) diff --git a/internal/service/store/store.go b/internal/service/store/store.go index 92eeb57..16c4d8d 100644 --- a/internal/service/store/store.go +++ b/internal/service/store/store.go @@ -3,11 +3,14 @@ package store import ( "errors" "fmt" + "github.com/patrickmn/go-cache" + "strconv" "sync" "sync/atomic" "texas-poker-bk/api" "texas-poker-bk/internal/game" "texas-poker-bk/internal/session" + "time" ) var ( @@ -18,9 +21,58 @@ var ( // NetAccounts 存储一下在线用户, TODO 连接终端,未在牌局或牌局未开始中回收账号,机器人代打后回收账号 NetAccounts map[int64]*session.NetAccount accountLock *sync.Mutex + + // 存储登录 token version + //TokenVersionStore map[int64]int32 + //tokenVersionLock *sync.Mutex + + // 后面使用多个缓存实体,减少不同类型锁时间 + storeCache *cache.Cache + // a. cache in one -> type conv + // b. cache in many -> space/code more + TokenVStore *store[int64, int32] ) +const ( + NoExpiration time.Duration = cache.NoExpiration + DefaultExpiration time.Duration = cache.DefaultExpiration +) + +type store[K any, V any] struct { + c *cache.Cache + zeroValue V + k2str func(K) string +} + +func (s *store[K, V]) SetDefault(k K, v V) { + s.c.SetDefault(s.k2str(k), v) +} + +func (s *store[K, V]) Set(k K, v V, d time.Duration) { + s.c.Set(s.k2str(k), v, d) +} + +func (s *store[K, V]) Get(k K) V { + v, found := s.c.Get(s.k2str(k)) + if !found { + return s.zeroValue + } + return v +} + +func (s *store[K, V]) Delete(k K) { + s.c.Delete(s.k2str(k)) +} + func init() { + TokenVStore = &store[int64, int32]{ + c: cache.New(cache.NoExpiration, cache.NoExpiration), + zeroValue: 0, + k2str: func(k int64) string { + return strconv.FormatInt(k, 10) + }, + } + TableNo = &atomic.Int32{} // 牌桌编号计数器 LobbyTables = make(map[int32]*game.Table, 16)