2 Star 0 Fork 1

taotechip / modules

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
redis.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
kyugao 提交于 2021-04-15 00:55 . 初始化
package cache
import (
"fmt"
"github.com/gomodule/redigo/redis"
"time"
)
var pool redis.Pool
func init() {
address := fmt.Sprintf("%s:%s", Config.Ip, Config.Port)
var err error
pool = redis.Pool{
Dial: func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", address)
if err != nil {
panic(err)
}
return
},
TestOnBorrow: nil,
MaxIdle: 3,
MaxActive: 20,
IdleTimeout: 240 * time.Second,
Wait: false,
MaxConnLifetime: 0,
}
if err != nil {
panic(err)
}
}
func GetConn() (conn redis.Conn) {
conn = GetConnWithNum(Config.Db)
return
}
func GetConnWithNum(dbNum string) (conn redis.Conn) {
conn = pool.Get()
conn.Do("SELECT", dbNum)
return
}
func Expire(key string, seconds int) (err error) {
connection := GetConn()
defer connection.Close()
err = expire(connection, key, seconds)
return
}
func expire(conn redis.Conn, key string, seconds int) (err error) {
_, err = conn.Do("EXPIRE", key, seconds)
return
}
func Exist(key string) (result bool, err error) {
connection := GetConn()
defer connection.Close()
result, err = exist(connection, key)
return
}
func exist(conn redis.Conn, key string) (result bool, err error) {
result, err = redis.Bool(conn.Do("EXISTS", key))
return
}
func Set(key string, value interface{}) (err error) {
connection := GetConn()
defer connection.Close()
set(connection, key, value)
return
}
func set(conn redis.Conn, key string, value interface{}) (err error) {
_, err = conn.Do("SET", key, value)
return
}
func HSet(key string, field string, value interface{}) (err error) {
connection := GetConn()
defer connection.Close()
err = hSet(connection, key, field, value)
return
}
func hSet(conn redis.Conn, key string, field string, value interface{}) (err error) {
_, err = conn.Do("HSET", key, field, value)
return
}
func HGet(key string, field string) (result interface{}, err error) {
connection := GetConn()
defer connection.Close()
result, err = hGet(connection, key, field)
return
}
func hGet(conn redis.Conn, key string, field string) (result interface{}, err error) {
result, err = conn.Do("HGET", key, field)
return
}
func HDel(key string, field string) (err error) {
connection := GetConn()
defer connection.Close()
err = hDel(connection, key, field)
return
}
func hDel(conn redis.Conn, key string, field string) (err error) {
_, err = conn.Do("HDEL", key, field)
return
}
func SetEx(key string, seconds int, value interface{}) (err error) {
connection := GetConn()
defer connection.Close()
err = setEx(connection, key, seconds, value)
return
}
func setEx(conn redis.Conn, key string, seconds int, value interface{}) (err error) {
_, err = conn.Do("SETEX", key, seconds, value)
return
}
func Get(key string) (value interface{}, err error) {
connection := GetConn()
defer connection.Close()
value, err = redis.String(connection.Do("GET", key))
return
}
func Del(key string) (err error) {
connection := GetConn()
defer connection.Close()
_, err = connection.Do("DEL", key)
return
}
1
https://gitee.com/taotechip/modules.git
git@gitee.com:taotechip/modules.git
taotechip
modules
modules
v0.5.5

搜索帮助

53164aa7 5694891 3bd8fe86 5694891