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.
61 lines
1.2 KiB
61 lines
1.2 KiB
4 years ago
|
package dbc
|
||
|
|
||
|
import (
|
||
|
"github.com/go-redis/redis"
|
||
|
"log"
|
||
|
"recook/configs"
|
||
|
"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 (
|
||
|
Rds *redis.Client
|
||
|
)
|
||
|
|
||
|
func SetupRedis() {
|
||
|
addr := "127.0.0.1:6379"
|
||
|
password := ""
|
||
|
|
||
|
if configs.IsProductionEnv() {
|
||
|
addr = "r-uf69300bab2e6fc4.redis.rds.aliyuncs.com:6379"
|
||
|
password = "TzsJtleGeOFKlw1w"
|
||
|
}
|
||
|
|
||
|
c := &RedisConfig{
|
||
|
Name: "recook.redis",
|
||
|
Addr: addr,
|
||
|
Idle: 10,
|
||
|
Active: 10,
|
||
|
DialTimeout: 1,
|
||
|
ReadTimeout: 1,
|
||
|
WriteTimeout: 1,
|
||
|
IdleTimeout: 10,
|
||
|
}
|
||
|
|
||
|
Rds = 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 := Rds.Ping().Result()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
log.Println(Rds)
|
||
|
}
|