owpengram-server/internal/store/redisstore/redis.go
2026-09-09 02:49:30 +03:00

35 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package redisstore 用 Redis 实现高频、易失且可重建的短状态、缓存、计数器与限流。
//
// 职责边界见 docs/persistence-layer.md §1Redis 存「态与计数」,丢失可由 PG/协议恢复。
package redisstore
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
const defaultOpenPingTimeout = 5 * time.Second
// Open 按地址建立 Redis 连接并用有界 PING 验证;返回的客户端继续保留 go-redis 默认重试策略。
func Open(ctx context.Context, addr, password string, db int) (*redis.Client, error) {
return open(ctx, addr, password, db, defaultOpenPingTimeout)
}
func open(ctx context.Context, addr, password string, db int, pingTimeout time.Duration) (*redis.Client, error) {
c := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
ContextTimeoutEnabled: true,
})
pingCtx, cancel := context.WithTimeout(ctx, pingTimeout)
defer cancel()
if err := c.Ping(pingCtx).Err(); err != nil {
_ = c.Close()
return nil, fmt.Errorf("redis ping %s: %w", addr, err)
}
return c, nil
}