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.

155 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package RedisUtil
import (
"dsTools/Utils/ConfigUtil"
"fmt"
garybudRedis "github.com/garyburd/redigo/redis"
"github.com/go-redis/redis/v7"
"github.com/sirupsen/logrus"
"time"
)
var (
// 定义redis链接池
RedisClient *redis.Client
Pool *garybudRedis.Pool
)
func init() {
// 从配置文件获取redis的ip以及db
var redisHost = ConfigUtil.RedisIp + ":" + ConfigUtil.RedisPort
Pool = &garybudRedis.Pool{
Dial: func() (garybudRedis.Conn, error) {
conn, err := garybudRedis.Dial("tcp", redisHost)
if err != nil {
return nil, err
}
return conn, nil
},
}
RedisClient = redis.NewClient(&redis.Options{
Addr: redisHost, // Redis地址
Password: "", // Redis账号
DB: 0, // Redis库
PoolSize: 100, // Redis连接池大小
MaxRetries: 3, // 最大重试次数
IdleTimeout: 10 * time.Second, // 空闲链接超时时间
})
pong, err := RedisClient.Ping().Result()
if err == redis.Nil {
fmt.Println("Redis异常")
} else if err != nil {
fmt.Println("失败:", err)
} else {
fmt.Println(pong)
}
}
//======下面的代码由黄海增加于2020-02-18=============================================================
/**
功能获取redis中的Hash缓存
作者:黄海
时间2020-02-18
*/
func HGETALL(key string) map[string]interface{} {
_map, err := RedisClient.HGetAll(key).Result()
n := make(map[string]interface{})
for k, v := range _map {
n[k] = interface{}(v)
}
if err == nil {
return n
}
return nil
}
func HSET(key string, value interface{}) {
RedisClient.HSet(key, value)
}
func HGET(key string, field string) *redis.StringCmd {
return RedisClient.HGet(key, field)
}
/**
功能设置hash值
作者:黄海
时间2020-02-18
*/
func HMSET(key string, m map[string]interface{}) {
RedisClient.HMSet(key, m)
}
func HMGET(key string, fields ...string) map[string]interface{} {
resMap := make(map[string]interface{})
for _, field := range fields {
var result interface{}
val, err := RedisClient.HGet(key, fmt.Sprintf("%s", field)).Result()
if err == redis.Nil {
logrus.Errorln("Key Doesn't Exists:", field)
resMap[field] = result
} else if err != nil {
logrus.Errorln("Redis HMGet Error:", err)
resMap[field] = result
}
if val != "" {
resMap[field] = val
} else {
resMap[field] = result
}
}
return resMap
}
/**
功能:设置指定键值的过期时间
作者:黄海
时间2020-02-18
*/
func EXPIRE(key string, ttl time.Duration) {
//24小时过期
RedisClient.Expire(key, ttl)
}
/**
功能:是否存在指定的键值
作者:黄海
时间2020-03-18
*/
func EXISTS(key string) (int64, error) {
return RedisClient.Exists(key).Result()
}
/**
功能:设置指定键值的过期时间
作者:黄海
时间2020-02-18
*/
func DEL(key string) {
RedisClient.Del(key)
}
/**
功能设置一个STRING形式的缓存
作者:黄海
时间2020-02-25
*/
func SET(key string, value string, ttl time.Duration) {
RedisClient.Set(key, value, ttl)
}
/**
功能获取指定的KEY值
作者:黄海
时间2020-02-25
*/
func GET(key string) (string, error) {
var r = RedisClient.Get(key)
if r.Err() != nil {
return "", r.Err()
} else {
return r.Result()
}
}