feat(common): 重构 share 包并添加缓存扫描功能
- 移除了 sessionManager 结构体和相关方法 - 新增 cacheStore 结构体的 Scan 方法,用于扫描匹配模式的键 - 新增 cacheStore 结构体的 MGet 方法,用于批量获取多个键的值 - 优化了代码结构,提高了缓存操作的灵活性和效率
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"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 ShareManager=newSessionManager()
|
||||
|
||||
var ShareManager = newSessionManager()
|
||||
var (
|
||||
// ErrCacheMiss 表示缓存未命中
|
||||
ErrCacheMiss = gerror.New("缓存未找到")
|
||||
@@ -26,22 +26,6 @@ type cacheStore[T any] struct {
|
||||
prefix string // 缓存键前缀
|
||||
}
|
||||
|
||||
// newSessionStore 创建会话缓存实例
|
||||
func newSessionStore() *cacheStore[uint32] {
|
||||
return &cacheStore[uint32]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:session:",
|
||||
}
|
||||
}
|
||||
|
||||
// newUserOnlineStore 创建用户在线状态缓存实例
|
||||
func newUserOnlineStore() *cacheStore[uint16] {
|
||||
return &cacheStore[uint16]{
|
||||
manager: cool.CacheManager,
|
||||
prefix: "blazing:useronline:",
|
||||
}
|
||||
}
|
||||
|
||||
// 生成带前缀的缓存键
|
||||
func (s *cacheStore[T]) formatKey(key string) string {
|
||||
return s.prefix + strings.TrimSpace(key)
|
||||
@@ -126,56 +110,48 @@ 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] // 用户在线状态缓存
|
||||
}
|
||||
|
||||
// newSessionManager 创建会话管理器
|
||||
func newSessionManager() *sessionManager {
|
||||
return &sessionManager{
|
||||
sessionStore: newSessionStore(),
|
||||
userOnlineStore: newUserOnlineStore(),
|
||||
// Scan 扫描匹配模式的键(先获取所有键,再内存筛选)
|
||||
func (s *cacheStore[T]) Scan(ctx context.Context, pattern string) ([]string, error) {
|
||||
// 1. 获取所有键
|
||||
allKeys, err := s.manager.Keys(ctx)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrapf(err, "获取所有键失败")
|
||||
}
|
||||
|
||||
// 2. 筛选符合模式的键(使用字符串匹配,支持简单通配符*)
|
||||
var matchedKeys []string
|
||||
for _, key := range allKeys {
|
||||
// 简单模式匹配:支持*匹配任意字符(可根据需求扩展为glob匹配)
|
||||
if matchSimplePattern(key.(string), pattern) {
|
||||
matchedKeys = append(matchedKeys, key.(string))
|
||||
}
|
||||
}
|
||||
return matchedKeys, nil
|
||||
}
|
||||
|
||||
// GetSession 通过会话ID获取用户ID
|
||||
func (m *sessionManager) GetSession(sessionID string) (uint32, error) {
|
||||
return m.sessionStore.Get(context.Background(), sessionID)
|
||||
// 简单模式匹配(支持*通配符)
|
||||
func matchSimplePattern(str, pattern string) bool {
|
||||
// 替换pattern中的*为正则匹配符,转义其他特殊字符
|
||||
regexPattern := strings.ReplaceAll(regexp.QuoteMeta(pattern), "\\*", ".*")
|
||||
regexPattern = "^" + regexPattern + "$" // 全匹配
|
||||
matched, _ := regexp.MatchString(regexPattern, str)
|
||||
return matched
|
||||
}
|
||||
|
||||
// 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 uint16) error {
|
||||
return m.userOnlineStore.Set(gctx.New(), gconv.String(userID), serverID, 0)
|
||||
}
|
||||
|
||||
// GetUserOnline 获取用户在线状态
|
||||
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 {
|
||||
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))
|
||||
// MGet 批量获取多个键的值(循环调用Get实现)
|
||||
func (s *cacheStore[T]) MGet(ctx context.Context, keys []string) ([]interface{}, error) {
|
||||
values := make([]interface{}, len(keys))
|
||||
for i, key := range keys {
|
||||
formattedKey := s.formatKey(key) // 格式化键(添加前缀)
|
||||
result, err := s.manager.Get(ctx, formattedKey)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrapf(err, "批量获取缓存失败,键: %s", key)
|
||||
}
|
||||
if result.IsEmpty() {
|
||||
values[i] = nil // 未命中时存nil
|
||||
} else {
|
||||
values[i] = result.Val()
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user