refactor(common): 重构 common 模块
- 移除 global.go 文件 - 更新 player.go 中的玩家数据存储方式 - 删除 session.go 文件 - 调整 rpc.go 中的 RPC 客户端方法 - 更新 ServerEvent.go 中的会话管理 - 调整 controller 中的 Maincontroller 结构 - 更新 login.go 中的用户登录逻辑 - 调整 service 中的玩家数据获取方式 - 更新 admin/login.go 和 login.go 中的会话管理
This commit is contained in:
181
common/data/cache/cache.go
vendored
Normal file
181
common/data/cache/cache.go
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrCacheMiss 表示缓存未命中
|
||||
ErrCacheMiss = gerror.New("缓存未找到")
|
||||
// ErrTypeConvert 表示类型转换失败
|
||||
ErrTypeConvert = gerror.New("缓存值类型转换失败")
|
||||
)
|
||||
|
||||
// CacheStore 泛型缓存存储
|
||||
type CacheStore[T any] struct {
|
||||
manager *gcache.Cache // 缓存管理器
|
||||
prefix string // 缓存键前缀
|
||||
}
|
||||
|
||||
// NewSessionStore 创建会话缓存实例
|
||||
func NewSessionStore() *CacheStore[uint32] {
|
||||
return &CacheStore[uint32]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:session:",
|
||||
}
|
||||
}
|
||||
|
||||
// NewUserOnlineStore 创建用户在线状态缓存实例
|
||||
func NewUserOnlineStore() *CacheStore[uint32] {
|
||||
return &CacheStore[uint32]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:useronline:",
|
||||
}
|
||||
}
|
||||
|
||||
// 生成带前缀的缓存键
|
||||
func (s *CacheStore[T]) formatKey(key string) string {
|
||||
return s.prefix + strings.TrimSpace(key)
|
||||
}
|
||||
|
||||
// Get 通过键获取缓存值
|
||||
func (s *CacheStore[T]) Get(ctx context.Context, key string) (T, error) {
|
||||
var zero T
|
||||
result, err := s.manager.Get(ctx, s.formatKey(key))
|
||||
if err != nil {
|
||||
return zero, gerror.Wrapf(err, "获取缓存失败,键: %s", key)
|
||||
}
|
||||
if result.IsEmpty() {
|
||||
return zero, ErrCacheMiss
|
||||
}
|
||||
|
||||
// 使用 ConvertWithRefer 进行类型转换
|
||||
value := gconv.ConvertWithRefer(result.Val(), zero)
|
||||
|
||||
// 类型断言检查转换结果
|
||||
converted, ok := value.(T)
|
||||
if !ok {
|
||||
return zero, gerror.Wrapf(
|
||||
ErrTypeConvert,
|
||||
"键: %s,缓存值实际类型: %T,期望类型: %T",
|
||||
key, result.Val(), zero,
|
||||
)
|
||||
}
|
||||
|
||||
return converted, nil
|
||||
}
|
||||
|
||||
// Set 设置缓存值并带有效期
|
||||
func (s *CacheStore[T]) Set(ctx context.Context, key string, value T, duration time.Duration) error {
|
||||
err := s.manager.Set(ctx, s.formatKey(key), value, duration)
|
||||
if err != nil {
|
||||
return gerror.Wrapf(err, "设置缓存失败,键: %s,值: %v", key, value)
|
||||
}
|
||||
fmt.Printf("[INFO] 缓存操作 [%s] 键: %s 值: %v 有效期: %v\n",
|
||||
s.prefix, key, value, duration)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Del 删除缓存
|
||||
func (s *CacheStore[T]) Del(ctx context.Context, key string) error {
|
||||
_, err := s.manager.Remove(ctx, s.formatKey(key))
|
||||
if err != nil {
|
||||
return gerror.Wrapf(err, "删除缓存失败,键: %s", key)
|
||||
}
|
||||
fmt.Printf("[INFO] 删除缓存 [%s] 键: %s 成功\n", s.prefix, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Contains 检查缓存是否存在
|
||||
func (s *CacheStore[T]) Contains(ctx context.Context, key string) (bool, error) {
|
||||
exists, err := s.manager.Contains(ctx, s.formatKey(key))
|
||||
if err != nil {
|
||||
return false, gerror.Wrapf(err, "检查缓存是否存在失败,键: %s", key)
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// GetOrSet 获取缓存值,如果不存在则设置默认值
|
||||
func (s *CacheStore[T]) GetOrSet(ctx context.Context, key string, defaultValue T, duration time.Duration) (T, error) {
|
||||
var zero T
|
||||
result, err := s.manager.GetOrSet(ctx, s.formatKey(key), defaultValue, duration)
|
||||
if err != nil {
|
||||
return zero, gerror.Wrapf(err, "获取或设置缓存失败,键: %s", key)
|
||||
}
|
||||
|
||||
// 类型转换
|
||||
value := gconv.ConvertWithRefer(result.Val(), zero)
|
||||
converted, ok := value.(T)
|
||||
if !ok {
|
||||
return zero, gerror.Wrapf(
|
||||
ErrTypeConvert,
|
||||
"键: %s,缓存值实际类型: %T,期望类型: %T",
|
||||
key, result.Val(), zero,
|
||||
)
|
||||
}
|
||||
|
||||
return converted, nil
|
||||
}
|
||||
|
||||
// SessionManager 会话管理器
|
||||
type SessionManager struct {
|
||||
sessionStore *CacheStore[uint32] // 会话缓存
|
||||
userOnlineStore *CacheStore[uint32] // 用户在线状态缓存
|
||||
}
|
||||
|
||||
// NewSessionManager 创建会话管理器
|
||||
func NewSessionManager() *SessionManager {
|
||||
return &SessionManager{
|
||||
sessionStore: NewSessionStore(),
|
||||
userOnlineStore: NewUserOnlineStore(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetSession 通过会话ID获取用户ID
|
||||
func (m *SessionManager) GetSession(sessionID string) (uint32, error) {
|
||||
return m.sessionStore.Get(context.Background(), sessionID)
|
||||
}
|
||||
|
||||
// SaveSession 保存会话信息
|
||||
func (m *SessionManager) SaveSession(sessionID string, userID uint32) error {
|
||||
return m.sessionStore.Set(gctx.New(), sessionID, userID, time.Hour*24)
|
||||
}
|
||||
|
||||
// DeleteSession 删除会话
|
||||
func (m *SessionManager) DeleteSession(sessionID string) error {
|
||||
return m.sessionStore.Del(gctx.New(), sessionID)
|
||||
}
|
||||
|
||||
// SessionExists 检查会话是否存在
|
||||
func (m *SessionManager) SessionExists(sessionID string) (bool, error) {
|
||||
return m.sessionStore.Contains(context.Background(), sessionID)
|
||||
}
|
||||
|
||||
// SetUserOnline 设置用户在线状态
|
||||
func (m *SessionManager) SetUserOnline(userID uint32, serverID uint32) error {
|
||||
return m.userOnlineStore.Set(gctx.New(), gconv.String(userID), serverID, 0)
|
||||
}
|
||||
|
||||
// GetUserOnline 获取用户在线状态
|
||||
func (m *SessionManager) GetUserOnline(userID uint32) (uint32, error) {
|
||||
return m.userOnlineStore.Get(context.Background(), gconv.String(userID))
|
||||
}
|
||||
|
||||
// DeleteUserOnline 删除用户在线状态
|
||||
func (m *SessionManager) DeleteUserOnline(userID uint32) error {
|
||||
return m.userOnlineStore.Del(gctx.New(), gconv.String(userID))
|
||||
}
|
||||
|
||||
// UserOnlineExists 检查用户在线状态是否存在
|
||||
func (m *SessionManager) UserOnlineExists(userID uint32) (bool, error) {
|
||||
return m.userOnlineStore.Contains(context.Background(), gconv.String(userID))
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"blazing/common/core"
|
||||
"blazing/cool"
|
||||
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
@@ -48,10 +48,8 @@ func (p *Player) SendPack(b []byte) error {
|
||||
|
||||
func ConutPlayer() int {
|
||||
|
||||
// v := reflect.ValueOf(&core.Mainplayer).Elem().FieldByName("m").Elem()
|
||||
// return int(v.FieldByName("count").Int())
|
||||
count := 0
|
||||
core.Mainplayer.Range(func(key, value interface{}) bool {
|
||||
cool.Mainplayer.Range(func(key, value interface{}) bool {
|
||||
count++
|
||||
return true // 继续遍历
|
||||
})
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
var sessionprx = "blazing:session:"
|
||||
|
||||
// GetSession 通过给定的键从缓存中获取会话数据。
|
||||
// 该函数主要执行以下操作:
|
||||
// 1. 使用 strings.Trim 移除输入字符串 t1 两端的空格,得到 t2。
|
||||
// 2. 调用 cool.CacheManager.Get 方法,在上下文为 context.Background() 的情况下,使用 t2 作为键从缓存中获取数据。
|
||||
// 参数:
|
||||
//
|
||||
// t1 - 用于获取会话数据的键字符串。
|
||||
func GetSession(t1 string) (userid uint32, err error) {
|
||||
// 移除输入键两端的空格,以确保键的格式一致性。
|
||||
t2 := strings.Trim(t1, " ")
|
||||
|
||||
// 从缓存中获取与键 t2 关联的数据。这里假设 cool.CacheManager 已经初始化,并且 Get 方法可用。
|
||||
// 此处不处理 err,可能是因为上层调用者期望处理这个错误,或者在特定上下文中,错误被视为可接受。
|
||||
t, err := cool.CacheManager.Get(context.Background(), sessionprx+t2)
|
||||
|
||||
userid = t.Uint32()
|
||||
return
|
||||
}
|
||||
func SaveSession(session string, userid string) error {
|
||||
|
||||
err := cool.CacheManager.Set(gctx.New(), sessionprx+strings.Trim(session, " "), userid, time.Hour*24)
|
||||
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
||||
//t, err := cool.CacheManager.Contains(context.Background(), strings.Trim(session, " "))
|
||||
|
||||
fmt.Println("前端获取", session, err)
|
||||
// if t {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user