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.
54 lines
1.1 KiB
54 lines
1.1 KiB
package RedisUtil
|
|
|
|
import (
|
|
"dsSzxy/Utils/ConfigUtil"
|
|
"github.com/go-redis/redis"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
RedisClient *redis.Client
|
|
RedisHost string
|
|
RedisDb int
|
|
)
|
|
|
|
func init() {
|
|
// 从配置文件获取redis的ip以及db
|
|
RedisHost = ConfigUtil.RedisIp + ":" + ConfigUtil.RedisPort
|
|
RedisClient = redis.NewClient(&redis.Options{
|
|
Addr: RedisHost,
|
|
DB: 0, // use default DB
|
|
})
|
|
}
|
|
|
|
func SET(key string, value string, expiration time.Duration) {
|
|
RedisClient.Set(key, value, expiration*time.Second)
|
|
}
|
|
|
|
func GET(key string) (string, error) {
|
|
val, err := RedisClient.Get(key).Result()
|
|
return val, err
|
|
}
|
|
|
|
func EXPIRE(key string, expiration time.Duration) {
|
|
RedisClient.Expire(key, expiration*time.Second)
|
|
}
|
|
|
|
func DEL(key string) {
|
|
RedisClient.Del(key)
|
|
}
|
|
|
|
func HMSET(key string, fields map[string]interface{}) {
|
|
RedisClient.HMSet(key, fields)
|
|
}
|
|
|
|
func HMGETALL(key string) map[string]string {
|
|
resMap, _ := RedisClient.HGetAll(key).Result()
|
|
return resMap
|
|
}
|
|
|
|
func HMGET(key string, fields []string) ([]interface{},error) {
|
|
resMap, err := RedisClient.HMGet(key, fields...).Result()
|
|
return resMap,err
|
|
}
|