refactor(common): 重构缓存管理模块,将cache迁移至share包并实现泛型缓存存储
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package cache
|
||||
package share
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var ShareManager=newSessionManager()
|
||||
var (
|
||||
// ErrCacheMiss 表示缓存未命中
|
||||
ErrCacheMiss = gerror.New("缓存未找到")
|
||||
@@ -20,35 +20,35 @@ var (
|
||||
ErrTypeConvert = gerror.New("缓存值类型转换失败")
|
||||
)
|
||||
|
||||
// CacheStore 泛型缓存存储
|
||||
type CacheStore[T any] struct {
|
||||
// cacheStore 泛型缓存存储
|
||||
type cacheStore[T any] struct {
|
||||
manager *gcache.Cache // 缓存管理器
|
||||
prefix string // 缓存键前缀
|
||||
}
|
||||
|
||||
// NewSessionStore 创建会话缓存实例
|
||||
func NewSessionStore() *CacheStore[uint32] {
|
||||
return &CacheStore[uint32]{
|
||||
// newSessionStore 创建会话缓存实例
|
||||
func newSessionStore() *cacheStore[uint32] {
|
||||
return &cacheStore[uint32]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:session:",
|
||||
}
|
||||
}
|
||||
|
||||
// NewUserOnlineStore 创建用户在线状态缓存实例
|
||||
func NewUserOnlineStore() *CacheStore[uint16] {
|
||||
return &CacheStore[uint16]{
|
||||
// newUserOnlineStore 创建用户在线状态缓存实例
|
||||
func newUserOnlineStore() *cacheStore[uint16] {
|
||||
return &cacheStore[uint16]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:useronline:",
|
||||
}
|
||||
}
|
||||
|
||||
// 生成带前缀的缓存键
|
||||
func (s *CacheStore[T]) formatKey(key string) string {
|
||||
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) {
|
||||
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 {
|
||||
@@ -75,7 +75,7 @@ func (s *CacheStore[T]) Get(ctx context.Context, key string) (T, error) {
|
||||
}
|
||||
|
||||
// Set 设置缓存值并带有效期
|
||||
func (s *CacheStore[T]) Set(ctx context.Context, key string, value T, duration time.Duration) error {
|
||||
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)
|
||||
@@ -86,7 +86,7 @@ func (s *CacheStore[T]) Set(ctx context.Context, key string, value T, duration t
|
||||
}
|
||||
|
||||
// Del 删除缓存
|
||||
func (s *CacheStore[T]) Del(ctx context.Context, key string) error {
|
||||
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)
|
||||
@@ -96,7 +96,7 @@ func (s *CacheStore[T]) Del(ctx context.Context, key string) error {
|
||||
}
|
||||
|
||||
// Contains 检查缓存是否存在
|
||||
func (s *CacheStore[T]) Contains(ctx context.Context, key string) (bool, error) {
|
||||
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)
|
||||
@@ -105,7 +105,7 @@ func (s *CacheStore[T]) Contains(ctx context.Context, key string) (bool, error)
|
||||
}
|
||||
|
||||
// GetOrSet 获取缓存值,如果不存在则设置默认值
|
||||
func (s *CacheStore[T]) GetOrSet(ctx context.Context, key string, defaultValue T, duration time.Duration) (T, error) {
|
||||
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 {
|
||||
@@ -126,56 +126,56 @@ func (s *CacheStore[T]) GetOrSet(ctx context.Context, key string, defaultValue T
|
||||
return converted, nil
|
||||
}
|
||||
|
||||
// SessionManager 会话管理器
|
||||
type SessionManager struct {
|
||||
sessionStore *CacheStore[uint32] // 会话缓存
|
||||
userOnlineStore *CacheStore[uint16] // 用户在线状态缓存
|
||||
// sessionManager 会话管理器
|
||||
type sessionManager struct {
|
||||
sessionStore *cacheStore[uint32] // 会话缓存
|
||||
userOnlineStore *cacheStore[uint16] // 用户在线状态缓存
|
||||
}
|
||||
|
||||
// NewSessionManager 创建会话管理器
|
||||
func NewSessionManager() *SessionManager {
|
||||
return &SessionManager{
|
||||
sessionStore: NewSessionStore(),
|
||||
userOnlineStore: NewUserOnlineStore(),
|
||||
// newSessionManager 创建会话管理器
|
||||
func newSessionManager() *sessionManager {
|
||||
return &sessionManager{
|
||||
sessionStore: newSessionStore(),
|
||||
userOnlineStore: newUserOnlineStore(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetSession 通过会话ID获取用户ID
|
||||
func (m *SessionManager) GetSession(sessionID string) (uint32, error) {
|
||||
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 {
|
||||
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 {
|
||||
func (m *sessionManager) DeleteSession(sessionID string) error {
|
||||
return m.sessionStore.Del(gctx.New(), sessionID)
|
||||
}
|
||||
|
||||
// SessionExists 检查会话是否存在
|
||||
func (m *SessionManager) SessionExists(sessionID string) (bool, error) {
|
||||
func (m *sessionManager) SessionExists(sessionID string) (bool, error) {
|
||||
return m.sessionStore.Contains(context.Background(), sessionID)
|
||||
}
|
||||
|
||||
// SetUserOnline 设置用户在线状态
|
||||
func (m *SessionManager) SetUserOnline(userID uint32, serverID uint16) error {
|
||||
func (m *sessionManager) SetUserOnline(userID uint32, serverID uint16) error {
|
||||
return m.userOnlineStore.Set(gctx.New(), gconv.String(userID), serverID, 0)
|
||||
}
|
||||
|
||||
// GetUserOnline 获取用户在线状态
|
||||
func (m *SessionManager) GetUserOnline(userID uint32) (uint16, error) {
|
||||
func (m *sessionManager) GetUserOnline(userID uint32) (uint16, error) {
|
||||
return m.userOnlineStore.Get(context.Background(), gconv.String(userID))
|
||||
}
|
||||
|
||||
// DeleteUserOnline 删除用户在线状态
|
||||
func (m *SessionManager) DeleteUserOnline(userID uint32) error {
|
||||
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) {
|
||||
func (m *sessionManager) UserOnlineExists(userID uint32) (bool, error) {
|
||||
return m.userOnlineStore.Contains(context.Background(), gconv.String(userID))
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"blazing/common/data/cache"
|
||||
"blazing/common/data/share"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -27,7 +27,7 @@ type ServerHandler struct{}
|
||||
// 实现踢人
|
||||
func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
|
||||
|
||||
useid1, err := cache.NewSessionManager().GetUserOnline(userid)
|
||||
useid1, err := share.ShareManager.GetUserOnline(userid)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("user not found")
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"blazing/common/data/cache"
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/data/share"
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
@@ -49,7 +49,8 @@ func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
|
||||
t := v.GetPlayer()
|
||||
if t != nil {
|
||||
glog.Debug(context.Background(), t.UserID, "断开连接")
|
||||
cache.NewSessionManager().DeleteUserOnline(t.UserID) //设置用户登录服务器
|
||||
|
||||
share.ShareManager.DeleteUserOnline(t.UserID) //设置用户登录服务器
|
||||
}
|
||||
|
||||
//}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blazing/common/data/cache"
|
||||
"blazing/common/data/share"
|
||||
"blazing/logic/service"
|
||||
"blazing/logic/service/login"
|
||||
"encoding/hex"
|
||||
@@ -18,7 +18,8 @@ func (h *Controller) Login(data login.LoginSidInfo, c gnet.Conn) []byte { //这
|
||||
|
||||
// h.RPCClient.UserLogin(int32(h.Port), int32(data.Head.UserID)) //初始化用户登录
|
||||
service.SetPlayer(c, data.Head.UserID)
|
||||
cache.NewSessionManager().SetUserOnline(data.Head.UserID, h.Port) //设置用户登录服务器
|
||||
|
||||
share.ShareManager.SetUserOnline(data.Head.UserID, h.Port) //设置用户登录服务器
|
||||
}
|
||||
|
||||
t1, _ := hex.DecodeString("0000045D37000003E9000186A600000000000186A6683F89CF6E69656F0000000000000000000000000008000F00000000000000000000000000000000000000000000000000000001000001DB0000018B000000000000A8C000000000000000000000000000000000000000080001388000000001000000017FFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000000000000000000000000000000000064000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001FFFFFFFF000000004E4F4E4F0000000000000000000000000000000000000001000000010000000100000001000000010000000100000001000000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000100000064000000000000000000000000000000000000001F000000000000006400000000000093F4000093F4000000D5000000F7000000AD00000088000000920000008C0000009C00000000000000000000000000000000000000000000000000000004000027900000001B00004E6200000014000028380000002800004E3E0000002368493DC60000000000000000000000000000000000000000000100000000000000A937000007D1000186A600000000000186A66E69656F00000000000000000000000000000000000000000000000F0000000000000000000001DB0000018B0000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"blazing/common/data/cache"
|
||||
"blazing/common/data/share"
|
||||
"blazing/common/socket/handler"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -21,7 +21,7 @@ func (l *LoginSidInfo) CheakSession() bool {
|
||||
//g.Dump(tt)
|
||||
t1 := hex.EncodeToString(l.Sid)
|
||||
|
||||
t, err := cache.NewSessionManager().GetSession(t1)
|
||||
t, err := share.ShareManager.GetSession(t1)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package consts
|
||||
Reference in New Issue
Block a user