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{} { resMap, _ := RedisClient.HMGet(key, fields...).Result() return resMap }