Browse Source

store with go-cache

master
tangmingyou 3 years ago
parent
commit
10eb14e77f
  1. 1
      data/config.toml
  2. 1
      go.mod
  3. 2
      go.sum
  4. 5
      internal/model/entity/user.go
  5. 1
      internal/service/auth.go
  6. 52
      internal/service/store/store.go

1
data/config.toml

@ -29,4 +29,3 @@ avatarPath = "data/avatar/"
[game.offline] # 玩家掉线配置
# 在房间中
outRoom = 15

1
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

2
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=

5
internal/model/entity/user.go

@ -5,9 +5,10 @@ type User struct {
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:余额
Balance int32 `gorm:"column:balance;default:0" json:"balance"` // comment:余额
Avatar string `gorm:"column:avatar" json:"avatar"` // comment:头像
Version int64 `gorm:"column:version" json:"version"` // 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 {

1
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()})

52
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)

Loading…
Cancel
Save