2026-04-01 00:58:42 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
|
|
|
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/config"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func NewClient(ctx context.Context, cfg config.RedisConfig) (*goredis.Client, error) {
|
2026-04-30 01:06:08 +08:00
|
|
|
client := NewLazyClient(cfg)
|
2026-04-01 00:58:42 +08:00
|
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("ping redis: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|
2026-04-30 01:06:08 +08:00
|
|
|
|
|
|
|
|
func NewLazyClient(cfg config.RedisConfig) *goredis.Client {
|
|
|
|
|
client := goredis.NewClient(&goredis.Options{
|
|
|
|
|
Addr: cfg.Addr,
|
|
|
|
|
DB: cfg.DB,
|
|
|
|
|
})
|
|
|
|
|
return client
|
|
|
|
|
}
|