This commit is contained in:
2025-06-17 11:16:24 +08:00
parent 6c3b4ec367
commit 759863f1aa
24 changed files with 103 additions and 92 deletions

View File

@@ -65,7 +65,7 @@ var PearJson = &dto.PearConfig{
},
},
Other: dto.Other{
KeepLoad: "200",
KeepLoad: "100",
AutoHead: false,
Footer: false,
},

View File

@@ -17,12 +17,15 @@ const redisTimeout = 200 * time.Millisecond
type RedisStore struct {
// 内嵌 go-redis 客户端
client *redis.Client
// 存储键前缀
prefix string
}
// NewRedisStore 是 RedisStore 的构造函数。
func NewRedisStore(client *redis.Client) *RedisStore {
func NewRedisStore(client *redis.Client, prefix string) *RedisStore {
return &RedisStore{
client: client,
prefix: prefix,
}
}
@@ -34,6 +37,8 @@ func (s *RedisStore) Find(token string) ([]byte, bool, error) {
// ✅ 必须: 无论函数如何返回,都调用 cancel() 来释放上下文资源
defer cancel()
token = s.prefix + token
// 使用 go-redis 的 Get 方法
data, err := s.client.Get(ctx, token).Bytes()
if err != nil {
@@ -59,6 +64,8 @@ func (s *RedisStore) Commit(token string, b []byte, expiry time.Time) error {
// time.Until(expiry) 会计算出当前时间到 expiry 之间的时间差
ttl := time.Until(expiry)
token = s.prefix + token
// 使用 go-redis 的 Set 方法,并设置过期时间
// 如果 expiry 时间已经过去ttl 会是负数Redis 会立即删除这个 key这正是我们期望的行为。
err := s.client.Set(ctx, token, b, ttl).Err()
@@ -76,6 +83,8 @@ func (s *RedisStore) Delete(token string) error {
// ✅ 必须: 无论函数如何返回,都调用 cancel() 来释放上下文资源
defer cancel()
token = s.prefix + token
// 使用 go-redis 的 Del 方法
err := s.client.Del(ctx, token).Err()
if err != nil {

View File

@@ -51,7 +51,8 @@ func NewSCSManager(client *redis.Client, conf *config.Config) (Manager, error) {
// github.com/alexedwards/scs/pgxstore
// sessionManager.Store = pgxstore.New(pool)
// redis
sessionManager.Store = NewRedisStore(client)
prefix := "m:scs:"
sessionManager.Store = NewRedisStore(client, prefix)
return &SCSSession{manager: sessionManager}, nil
}