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