You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.4 KiB

2 months ago
package redishandle
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
type RedisClient struct {
Client *redis.Client
}
func NewRedisClient(dsn, pwd string, db int) *RedisClient {
client := &RedisClient{
Client: redis.NewClient(&redis.Options{
Addr: dsn,
Password: pwd,
DB: db,
PoolSize: 20,
}),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := client.Client.Ping(ctx).Result(); err != nil {
panic(err)
}
return client
}
func InitRedis(dsn string, pwd string, dbs map[string]int) (map[string]*RedisClient, error) {
redisCaches := make(map[string]*RedisClient)
for k, v := range dbs {
redisCaches[k] = &RedisClient{
Client: redis.NewClient(&redis.Options{
Addr: dsn,
Password: pwd,
DB: v,
PoolSize: 20,
}),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := redisCaches[k].Client.Ping(ctx).Result()
if err != nil {
return nil, err
}
}
return redisCaches, nil
}
func InitRedisClient(dsn string, pwd string, database int) (*redis.Client, error) {
Client := redis.NewClient(&redis.Options{
Addr: dsn,
Password: pwd,
DB: database,
PoolSize: 20,
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := Client.Ping(ctx).Result()
if err != nil {
return nil, err
}
return Client, nil
}