This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
redis.go
60 lines (51 loc) · 1.59 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package redis
import (
"github.com/go-redis/redis"
"github.com/im-kulikov/helium/module"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type (
// Config alias
Config = redis.Options
// Client alias
Client = redis.Client
)
var (
// Module is default Redis client
Module = module.Module{
{Constructor: NewDefaultConfig},
{Constructor: NewConnection},
}
// ErrEmptyConfig when given empty options
ErrEmptyConfig = errors.New("missing redis config key")
)
// NewDefaultConfig for connection
func NewDefaultConfig(v *viper.Viper) (*Config, error) {
if !v.IsSet("redis.address") {
return nil, ErrEmptyConfig
}
return &Config{
Addr: v.GetString("redis.address"),
Password: v.GetString("redis.password"),
DB: v.GetInt("redis.db"),
MaxRetries: v.GetInt("redis.max_retries"),
MinRetryBackoff: v.GetDuration("redis.min_retry_backoff"),
MaxRetryBackoff: v.GetDuration("redis.max_retry_backoff"),
DialTimeout: v.GetDuration("redis.dial_timeout"),
ReadTimeout: v.GetDuration("redis.read_timeout"),
WriteTimeout: v.GetDuration("redis.write_timeout"),
PoolSize: v.GetInt("redis.pool_size"),
PoolTimeout: v.GetDuration("redis.pool_timeout"),
IdleTimeout: v.GetDuration("redis.idle_timeout"),
IdleCheckFrequency: v.GetDuration("redis.idle_check_frequency"),
}, nil
}
// NewConnection of redis client
func NewConnection(opts *Config) (cache *Client, err error) {
cache = redis.NewClient(opts)
if _, err = cache.Ping().Result(); err != nil {
return nil, err
}
return cache, nil
}