package dao
import (
"fmt"
"github.com/gomodule/redigo/redis"
"gopkg.in/ini.v1"
"os"
"sync"
"time"
)
var once sync.Once
// RedisClient Redis 服务
type RedisClient struct {
Client *redis.Pool
}
//Redis 全局 Redis
var RedisPool *RedisClient
//ConnectRedis 连接 redis 数据库,设置全局的 Redis 对象
func ConnectRedis() {
config, err := ini.Load("./config/app.ini")
if err != nil {
//失败
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
address := config.Section("redis").Key("address").String()
password := config.Section("redis").Key("password").String()
db, _ := config.Section("redis").Key("db").Int()
once.Do(func() {
RedisPool = NewClient(address, password, db)
})
con_err := RedisPool.Ping()
if con_err != nil {
panic(con_err)
}
}
// NewClient 创建一个新的 redis 连接
func NewClient(address string, password string, db int) *RedisClient {
// 初始化自定的 RedisClient 实例
rds := &RedisClient{}
// 使用 redis 库里的 NewClient 初始化连接
rds.Client = &redis.Pool{
MaxIdle: 100, //最大空闲
MaxActive: 1000, //最大连接
IdleTimeout: time.Duration(60) * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial(
"tcp",
address,
redis.DialPassword(password),
redis.DialDatabase(int(db)),
redis.DialConnectTimeout(time.Duration(60)*time.Second),
redis.DialReadTimeout(time.Duration(60)*time.Second),
redis.DialWriteTimeout(time.Duration(60)*time.Second),
)
if err != nil {
return nil, err
}
return c, err
},
}
return rds
}
// Ping 用以测试 redis 连接是否正常
func (rds *RedisClient) Ping() error {
_, err := rds.Client.Get().Do("ping")
return err
}
// Set 存储 key 对应的 value,且设置 expiration 过期时间(单位纳秒)
func (rds *RedisClient) Setex(key string, expiration int, value interface{}) bool {
conn := rds.Client.Get()
defer conn.Close()
if _, err := conn.Do("setex", key, expiration, value); err != nil {
fmt.Println(err)
return false
}
return true
}
//
//Get 获取 key 对应的 value
func (rds *RedisClient) Get(key string) string {
conn := rds.Client.Get()
defer conn.Close()
result, err := redis.String(conn.Do("Get", key))
if err != nil {
return ""
}
return result
}
//Get 获取 key 对应的 value
func (rds *RedisClient) Rpop(key string) (string, error) {
conn := rds.Client.Get()
defer conn.Close()
result, err := redis.String(conn.Do("Rpop", key))
if err != nil {
return "", err
}
return result, nil
}
|