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.
53 lines
1.1 KiB
53 lines
1.1 KiB
5 years ago
|
package db
|
||
|
|
||
|
import (
|
||
|
"github.com/go-redis/redis"
|
||
|
"live/app/lib/config"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type RedisConfig struct {
|
||
|
Name string `toml:"name"`
|
||
|
Proto string `toml:"proto"`
|
||
|
Addr string `toml:"addr"`
|
||
|
Idle int `toml:"idle"`
|
||
|
Active int `toml:"active"`
|
||
|
DialTimeout int `toml:"dialTimeout"`
|
||
|
ReadTimeout int `toml:"readTimeout"`
|
||
|
WriteTimeout int `toml:"writeTimeout"`
|
||
|
IdleTimeout int `toml:"idleTimeout"`
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
Redis *redis.Client
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
addr := config.Config.Section("redis").Key("addr").String()
|
||
|
password := config.Config.Section("redis").Key("password").String()
|
||
|
|
||
|
c := &RedisConfig{
|
||
|
Name: "recook.redis",
|
||
|
Addr: addr,
|
||
|
Idle: 10,
|
||
|
Active: 10,
|
||
|
DialTimeout: 1,
|
||
|
ReadTimeout: 1,
|
||
|
WriteTimeout: 1,
|
||
|
IdleTimeout: 10,
|
||
|
}
|
||
|
|
||
|
Redis = redis.NewClient(&redis.Options{
|
||
|
Addr: c.Addr,
|
||
|
Password: password,
|
||
|
DB: 0, // use default DB
|
||
|
ReadTimeout: time.Duration(c.ReadTimeout) * time.Second,
|
||
|
WriteTimeout: time.Duration(c.WriteTimeout) * time.Second,
|
||
|
})
|
||
|
|
||
|
_, err := Redis.Ping().Result()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|