代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。