32 lines
674 B
Go
32 lines
674 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"everypublish/server/internal/config"
|
|
)
|
|
|
|
// Redis 封装(D4 起 asynq 队列同源共用)
|
|
type Redis struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
// New 建立 Redis 连接
|
|
func New(cfg *config.Config) *Redis {
|
|
client := redis.NewClient(&redis.Options{Addr: cfg.RedisAddr, Password: cfg.RedisPass, DB: 0})
|
|
return &Redis{client: client}
|
|
}
|
|
|
|
// Ping 探测
|
|
func (r *Redis) Ping() error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
return r.client.Ping(ctx).Err()
|
|
}
|
|
|
|
// Client 暴露底层客户端
|
|
func (r *Redis) Client() *redis.Client { return r.client }
|